The following examples illustrate common JavaScript expressions.
To extract a substring of the first 15 characters of the CITY attribute value, use the following syntax:
record.getItem('/DEMO/CUSTOMERS/CITY').substr(0,15)To concatenate a value in the ADDRESS attribute with values in the CITY and STATE attributes, separating each value with a space, use the following syntax:
record.getItem('/DEMO/CUSTOMERS/ADDRESS')+ ' ' +record.getItem('/DEMO/CUSTOMERS/CITY')+ ' ' +record.getItem('/DEMO/CUSTOMERS/STATE')To prevent errors, use an if-else statement to ignore the substring method when the length of the attribute value is less than the length of the substring. In the following syntax, the substring method is not used if a value in CUSTNAME is less than or equal to 8 characters:
var maxLength = 8 if ( record.getItem('/DEMO/CUSTOMERS/CUSTNAME').toString().length() > maxLength ) { record.getItem('/DEMO/CUSTOMERS/CUSTNAME').substr( 0, maxLength ) } else { record.getItem('/DEMO/CUSTOMERS/CUSTNAME') }To return a random date in YYYY-MM-DD format (for use with the java.sql.Date class), use the Date() method to obtain the current date and the setDate() method to add a random number of days (from 0 to 365) to the date. Then concatenate the values returned by the getFullYear(), getMonth(), and getDate() methods to return the new date in YYYY-MM-DD format. Use the following syntax:
var dob=new Date(); dob.setDate(dob.getDate()+Math.floor(Math.random()*365)) dob.getFullYear()+'-'+dob.getMonth()+'-'+dob.getDate()