Scriptable service catalog variables
Summarize
Summary of Scriptable service catalog variables
ServiceNow enables you to script and reference service catalog request item variables directly from tables in both scoped and non-scoped environments. Variables must have a name to be accessible via JavaScript, and variables within variable sets can be referenced using either the variable or variable set name. Variable sets act as first-class entities with roles that override individual variable roles, controlling read, write, and create permissions.
Show less
Key Features
- Variable Access and Manipulation: Use
current.variables.<variablename>to get or set variable values. Example scripts show how to print, set, and use variables to create inventory items. - GlideRecord Variable APIs: Access variables associated with a GlideRecord using
nowGR.variablesand get variable values or lists of variables with methods likegetVariableValue()andgetElements(). - GlideElementVariable Methods: Useful methods include
isMultiRow()to check if a variable is a multi-row variable set,getQuestion()andgetLabel()for metadata,canRead()andcanWrite()for permission checks, andgetDecryptedValue()for masked variables. - Multi-row Variable Sets: Support for multi-row variable sets with operations to get/set values, count rows, access rows and cells, add empty rows, and delete rows. Variables within multi-row sets can be accessed and manipulated individually or collectively.
- Example Scripts: Provided examples demonstrate how to iterate over variables and multi-row variable sets on service catalog task records, logging labels and values for practical use.
Usage Considerations and Limitations
- Variables can only be set in before business rules; setting variables in after business rules does not persist changes to the database.
- Namespace collisions can occur if multiple variables share the same name, leading to overwriting.
- Date/time variables follow standard system storage and display rules, stored in GMT and shown in the user's local time zone.
Practical Benefits for ServiceNow Customers
By leveraging scriptable service catalog variables, ServiceNow customers can:
- Programmatically read and manipulate catalog item variables to automate asset creation and other processes.
- Access detailed variable metadata and permissions to build secure and dynamic catalog workflows.
- Work efficiently with complex multi-row variable sets to handle tabular data within catalog items.
- Ensure variable handling respects business rule execution timing to maintain data integrity.
You can use scripting to reference any request item variable from a table in scoped and non-scoped environment.
current.variables.<variable_name>When a variable is part of a variable set, you can reference it as current.variables.<variable_name> or current.variables.<variable_set_name>.<variable_name>.
Variable set is also a first-class citizen in Service Catalog. Like variables, a variable set has read, write, and create roles. If roles are provided for a variable set, the roles are applicable for the variables within the set. Roles of an individual variable are overridden by the roles of the variable set.
Print a variable
var original = current.variables.original_number;
gs.print(original);
Set a variable
current.variables.name = "Auto-Generated:" + current.variables.asset_tag;
Create an inventory item with fields set from variables
doCreation();
function doCreation ( ) {
var create = current.variables.create_item;
if (create == 'true') { // we want to create an asset
var computer = new GlideRecord('cmdb_ci_computer');
computer.initialize();
computer.asset_tag = current.variables.asset_tag;
computer.serial_number = current.variables.serial_number;
computer.name = current.variables.name;
computer.manufacturer = current.variables.company;
computer.insert(); } }
Get GlideElementVariable of variables and variable sets associated with a GlideRecord
now_GR.variables
Get the name value pair of variables associated with a GlideRecord
now_GR.variables.getVariableValue();
Get a list of GlideElementVariable for variables within a task record
now_GR.variables.getElements();
Get a list of GlideElementVariable for variables (including multi-row variable set) within a task record
now_GR.variables.getElements(true);
APIs for GlideElementVariable
- now_GR.variables.<var_name>.isMultiRow(): Get whether the GlideElementVariable is a multi-row variable set or a variable.
- now_GR.variables.<var_name>.getQuestion(): Get the Question object for a variable. Applicable only for a variable (isMultiRow() is false) and not for a multi-row variable set.
- now_GR.variables.<var_name>.getLabel(): Get the label of the GlideElementVariable. For a variable, the label of the variable is returned. For multi-row variable set, the title of the variable set is returned.
- now_GR.variables.<var_name>.canRead(): Get whether the user can view a variable or multi-row variable set.
- now_GR.variables.<var_name>.canWrite(): Get whether the user can edit a variable or multi-row variable set.
- now_GR.variables.<var_name>.getDecryptedValue(): Get the decrypted value for a masked variable. Applicable only for a masked variable.
- now_GR.variables.<var_name>.getRows(): Get the list of row objects for a multi-row variable set. Applicable only for a multi-row variable set (isMultiRow() is true).
- now_GR.variables.<var_name>.getRowCount(): Get the number of rows for multi-row variable set. Applicable only for a multi-row variable set (isMultiRow() is true).
Example to access variables of GlideRecord for the Task table
var now_GR = new GlideRecord('sc_req_item');
if (now_GR.get('635a1f5387320300e0ef0cf888cb0b73')) {
var variables = now_GR.variables.getElements();
for (var i=0;i<variables.length;i++) {
var question = variables[i].getQuestion();
gs.log(question.getLabel() + ":" + question.getValue())
}
}
Example to access a multi-row variable set of GlideRecord for the Task table
var now_GR = new GlideRecord('sc_req_item');
now_GR.get('02c38dcd87013300e0ef0cf888cb0bb2');
var vars = now_GR.variables.getElements(true);
for (var i=0; i<vars.length; i++) {
var now_V = vars[i];
if (now_V.isMultiRow()) {
var rows = now_V.getRows();
for (var j=0; j<now_V.getRowCount(); j++) {
var row = rows[j];
var cells = row.getCells();
for (var k=0; k<cells.length; k++) {
var cell = cells[k];
gs.info(cell.getLabel() + ":" + cell.getCellDisplayValue())
}
}
}
}
Multi-row variable set
| Operation | Usage |
|---|---|
| Table operations | |
| Return JSON array value as String | |
| Set value of a multi-row variable set |
Note: An array of ordered (key, value) pairs is also applicable as input. |
| Get value of column, var1, of a multi-row variable set | |
| Set value of a variable set, var1 |
Note: An array of ordered (key, value) pairs is also applicable as input. |
| Row operations | |
| Get the current row count | |
| Returns the row specified by the variable "i" - getRow(<int> i) | |
| Get the cell value for a question column mapped to <var_name> | |
| Set the cell value for a question column mapped to <var_name> | |
| Set the cell value for a question column mapped to <var_name> | |
| Add an empty row at the end of the table and return a scriptable object | |
| Delete a row | |
Notes and limitations
- //Single column of table_Var
- now_GR.variables.table_var.var1
- now_GR.variables.table_var.var1 = <val>
- You can only set a variable in a before business rule. Variables set in an after rule are not written to the database.
- There is nothing in place to prevent namespace collision with variables. Creating two variables named computer_speed would result in only one of them showing up; the second one would overwrite the first one.
- Date/time variables use the same time zone formatting and storage rules as all other dates in the system. They are stored internally in GMT, but translated into the user's local time zone and format for display.