Dynamic variable support in record screens
Summarize
Summary of Dynamic Variable Support in Record Screens
The dynamic variable support in record screens allows you to display dynamic field values in the mobile app using a script screen field. This enables personalized content, such as translated HR case descriptions, without altering the underlying database values.
Show less
Key Features
- Dynamic Field Values: Scripts can be used to determine and display field values before they appear in the form.
- Configuration: The script field for record screens is configured within the ServiceNow AI Platform, distinct from the Mobile App Builder.
- Execution Script: Scripts must return a JSON object containing a 'Type' (format) and 'Value' (content). Supported types include Auto, Text, Percentage, Date, and HTML.
- Date Formatting: Dates must follow specific formats to ensure correct display.
Key Outcomes
By implementing dynamic variable support, HR employees can view their cases in their preferred language, enhancing user experience on mobile devices. The configuration allows for personalized communication while maintaining data integrity.
For practical implementation, ensure that the script includes required properties and adheres to format guidelines for the type of content being displayed.
Use the script screen field in record screens to display dynamic field values. The script runs to determine the field value before it displays in your form. The value returned by the script doesn't replace the database value. For example, you can display translated content for dynamic variables within an email.
Use case
Employees can view their HR cases and tasks in their preferred language on mobile devices, even when descriptions include personalized information from other fields. Using the script screen field in record screens, you can display these translated dynamic values in the mobile app. This provides the option for employees to see accurate translations of their HR cases, even when the descriptions contain personalized information.
Items to note when using dynamic variable support
- In the Type field, select Script.
- In the Form field field, select a field that contains the dynamic variable you want to use.
Working with the Execution Script field
Use the following script format to return values for display on the mobile app. The script must return a JSON object with two required properties. Type defines the content format (in the example it's
HTML), and Value contains the actual content to display (in the example it's Hello).
var json = {
‘Type’: ‘HTML’,
‘Value’: “<p>Hello! </p>”
}
- The script must contain the properties
TypeandValue - The Type property can use the following options: Auto, Text, Percentage, Date, and HTML.
- The
Typein the JSON must be the same as the type selected from the field. - Date values must use this format: yyyy-MM-dd HH:mm:ss
Examples
- Example 1: Displaying personalized case descriptions
- Consider an HR case description field containing a variable that references the assigned user's first
name:
'Hi {current.assigned_to.first_name}, Unable to access the personal details section in payroll portal.'The script after this extracts the variable, retrieves the actual field value, and returns a personalized description:
The script returns the updated text with Type set to 'Text'. For HTML content, set the Type to 'HTML'.'Hi Beth, Unable to access the personal details section in payroll portal.'(function generateScreenField(current) { //Type appropriate comment here, and begin script below var description = current.description; var regex = /\$\{(.*?)\}/; var match = description.match(regex); var fieldName = match[1]; var element = current.getElement(fieldName); var fieldValue = element.getValue(); var updatedDescription = description.replace(regex, fieldValue); var json = { 'Type': 'Text', 'Value': updatedDescription }; return json; })(current); - Example 2: Calculating elapsed time as a percentage
- The script calculates the percentage of time elapsed between start and end dates and displays it in a percentage field on the details screen.Note:The percentage value returns with a numeric value and without the % symbol. The symbol is handled by the field formatting.
(function generateScreenField(current) { //Type appropriate comment here, and begin script below //Logic to calculate percentage of time elapsed between start and end dates //Return percentage without % symbol var json = { 'Type': 'Percentage', 'Value': timeElapsedPercent }; return json; })(current); - Example 3: Calculating date values dynamically
- This script calculates a date and displays it in a date field. The script runs when the field appears on the mobile app, calculates the date based on your logic, and returns it in the correct format.Date format requirements:
- Date only: yyyy-MM-dd (example: 2025-01-29)
- Date with time: yyyy-MM-dd HH:mm:ss (example: 2025-01-29 14:30:00)
(function generateScreenField(current) { //Type appropriate comment here, and begin script below //Logic to calculate value of the Date field //Return Date in the following format: yyyy-MM-dd (without Time) var json = { 'Type': 'Date', 'Value': calculatedDate }; (or) //Return DateTime in the following format: yyyy-MM-dd HH:mm:ss (with time) var json = { 'Type': 'DateTime', 'Value': calculatedDate }; return json; })(current);