Edit Content
Click on the Edit Content button to edit/add the content.

The Set Command

This command enables the template author to control the contents of various data elements in the Interview using a fairly simple syntax.

Set the date of birth

As an example, suppose you have an interview that requires the user to enter a number of items of personal information, such as first name, last name and date of birth. Here is a very simple template:

«FirstName»
«LastName»
«FormatDate(DateOfBirth,'o MMMM yyyy')»

Somewhat annoyingly, many systems will present a calendar for the capture of a date of birth which require the user to scroll or tab through a list of years starting with the current date. This is only useful when capturing a date of birth of a very young child. In the case of the user’s own date of birth, it is far more likely to be at least 20 years in the past. The Set command will help you present a nicer calendar starting point to the user:

«FirstName»
«LastName»
«FormatDate(DateOfBirth,'o MMMM yyyy')»

«Set(DateOfBirth,(DateOfBirth = ''),IncrementDate(Today(),-20,'y'))»

You can read the command as saying, “If the DateOfBirth data element is empty (DateOfBirth = ''), then set it to 20 years ago from today”.

The test for the data element being empty will permit the user to change the date, and the Set command will then leave it alone.

 

Calculate the age

Once you know the date of birth, then you can calculate the person’s age. The following will show how to calculate the age in years:

«Set(Age,,YearsBetween(Today(),DateOfBirth))»

The two commas after Age indicate that the Set is unconditional, in other words, the Age data element will ALWAYS have the value as calculated.

To see this in action, make a template like the above sample, and include the two Set commands, and an «Age» fillpoint. Then run the template. You should notice the following:

  • The DateOfBirth should start off as 20 years in the past.
  • The Age data element will have the value 20.

Try to change the Age. It should not change. At least, it will change and then revert to 20 once you exit the field in the interview.

Change the date to your own date of birth – the Age should then be your age, although it will probably include a fractional part. You could do the following to get the Age to be a bit more like we usually quote our age:

«Set(Age,,floor(YearsBetween(Today(),DateOfBirth)))»

Or, insurance companies like to use “age next birthday”, which would then be:

«Set(Age,,ceil(YearsBetween(Today(),DateOfBirth)))»

 

Related articles:

The With command

Table of Contents