Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

When writing a Calculated Value rule in Javascript, a system variable is predefined to receive the data called value.  The value variable must be set in the rule to assign the data into the component of interest.  As with most other Javascript rules, the data variable is also available and is used to access the values of all components on the form.

Code Block
languagejs
titleName Concatenation Rule
linenumberstrue
var fullName = ''; //Initialize a variable to store the data

if(data.firstName) //If there is data, assign it to fullName with a trailing space
    fullName = data.firstName + ' ';
    
if (data.middleInitial) //If there is data, append it to fullName with a trailing space
    fullName += data.middleInitial + ' ';
    
if (data.lastName) //If there is data, append it to full name
    fullName += data.lastName;

value = fullName.trim();  //Trim off any trailing spaces and assign the data to the 'value' variable

You can use any Javascript you are comfortable with when author rules like this.  The example above shows the basic sequential construction of a string.  The same thing can be written in a number of ways.  A more developer focused example of this rule is provided below.  Note that this code does the exact same thing as the example above.  Use whatever is easier or more understable for you.

Code Block
languagejs
titleAdvanced Name Concatenation Rule
linenumberstrue
var fullName = (data.firstName ? data.firstName + ' ' : '') + (data.middleInitial ? data.middleInitial + ' ' : '') + (data.lastName || '');

value = fullName.trim();