Use ServiceNow® Virtual Agent script methods and variables to write chat scripts, such as response, trigger, and flow scripts. Variables can also provide context for your live support topics and conversations.

Virtual Agent scripts can provide context for topics, such as retaining information about a user or a user's input. You can use this information to personalize a conversation, such as to present a scripted greeting or confirmation. Scripts can also specify certain actions to be performed on information that was obtained during a conversation. These actions can be used for creating or updating ServiceNow records.

Scripts run on the server in the scope in which a topic is defined. All scope protections apply. If you're developing a conversation for a scoped application, you must use the scoped API. You can use most APIs that run in server scripts.

User input and ServiceNow record variables

In Virtual Agent, the following variables are available for use in scripts that you create for a topic.

User input variables

When you add an input control to a topic, the system automatically creates a variable to store the user input (a string, Boolean value, and so on.). The syntax is vaInputs.myvar where myvar is the name that you assigned to the input node.

For example, a text input prompt with the name First prompt has a system-defined variable called vaInputs.first_prompt. Spaces in the name are replaced with an underscore character, and uppercase characters are changed to lowercase.

ServiceNow record variables

If you create a script for a topic that queries a ServiceNow table, then the record object (GlideRecord) returned is automatically available for use in the topic. The variable syntax is vaInputs.myvar where myvar is the record object. For details on querying ServiceNow tables to return a record object, see Querying tables in script.

Use dot-walking in variables that contain a ServiceNow record to specify a particular field in the table. The syntax is vaInputs.myvar.field. For details on dot-walking within a script, see Dot-walking examples.

Accessing user input and ServiceNow record variables

Use vaInputs objects to access ServiceNow record variables in scripts. For example, the vaInputs.myvar == "expected value". To access values from user inputs that are not records, use vaInputs.myvar.getValue().

The following methods are available.

Script variables

You can define script variables for information that is not stored in ServiceNow tables but that can be used to share that information elsewhere in a topic. Script variables are similar to workflow scratchpad variables that store primitives such as integers, Boolean values, or strings. A string value can be either a static or dynamic scripted value. Use the Variables sidebar to define script variables for a topic in Virtual Agent Designer.

Figure 2. Variables sidebar
Sidebar in the Virtual Agent Designer page for manually adding script variables that are used in topic scripts.
Note: Script variables are intended for use by topic authors with advanced scripting skills.

The variable syntax is vaVars.myvar, where myvar is the name that you assign to the variable. For example, you can assign a value to the variable using vaVars.myvar = value;. Unlike user input variables, script variables can be assigned values in a script.

For information about defining a script variable in a topic, see Define script variables for a topic.

Context variables

Context variables work in a global scope whereas system variables work within the scope of the topic. You can use context variables in system parameters within the web client URL. These variables can be used anywhere in the conversation using the vaContext object. Use these options to create links to Virtual Agent with predefined variables.

The following example link contains a parameter, sysparm_city=milan. In addition to opening a Virtual Agent conversation, using this link creates a variable called city with a value of milan. This variable can be accessed in a script using vaContext.city.

https://<instance>.service-now.com/$sn-va-web-client-app.do?sysparm_city=milan
A number of Live Agent variables are available for use in topic scripts. These variables include the following:
  • application
  • language
  • portal
  • search_text
  • short_description

The variable syntax is vaContext.LiveAgent_myvar, where myvar is one of the available live agent context variables. Access the variable using vaContext.LiveAgent_myvar = value;.

To use Live Agent variables in topic scripts, open the topic in Virtual Agent Designer. On the Properties tab, select the pencil icon next to Live Agent Variables to add them to the topic.

For information about live agent context variables that are included with Virtual Agent, see Live agent chat context variables.

vaSystem methods

You can use methods in the vaSystem object to do various tasks. Some of these tasks include the following:
  • Attach images to records.
  • Access the search string that was used to find the current topic.
  • Verify that a live agent is available to receive a conversation.
  • Connect the user to a live agent.
Note: The use of undocumented system methods is not recommended, as they are unsupported and may be discontinued or altered at any time without prior notice.
Table 3. Method that can be used outside Virtual Agent topics
Method Description
sn_cs.VASystemObject.getTranscriptById(<conversation sysId>)

Gets the transcript for the current Virtual Agent conversation.