Skip to content
Autofill Description from Template

Autofill Description from Template

Overview

Catalog items can store a dynamic template string (in custom fields u_dynamic_description_template and u_dynamic_short_description_template on the catalog item) that describes how to build the description and short_description of a resulting RITM from its variables. Two Business Rules — running on sc_req_item — read that template, substitute in variable values, and write the result back onto the record.

The templates are written in a small text mini-language with three tokens:

  • VAR.field — replaced with the raw value of the catalog variable named field.
  • REF.field(table) — treats field as a reference variable (a sys_id) and replaces the token with that record’s display value, looked up on table.
  • CONSW.field(value1:1,value2:2,...) — a “conditional switch.” It reads the current value of variable field, maps it to a numbered template block (value1 maps to block 1, value2 to block 2, etc.), and replaces the entire template with just that block before the VAR/REF substitution runs. Blocks are written inline as 1)...text..., 2)...text..., terminated by a trailing $).

Problem Statement

Different catalog items need very different description text, and a single item can even need different text depending on a variable’s value (e.g. a “Server Request” item behaves differently for “new server” vs. “decommission”). Hardcoding this logic per catalog item in separate business rules would be unmaintainable or would need to have custom flows for each catalog item instead of being able to use an single flow for many items. Instead, the wording lives as data template string stored on the catalog item and one generic business rule engine parses and fills it in, so admins can add or edit request-type wording without touching script.

If no template is defined at all, both rules fall back to a plain variable (current.variables.Description for the description, current.request_item.u_task_short_description for the short description).

Autofill Description.js
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
 * Script Name: Autofill Description
 * Description: This script automatically fills the description field of a ServiceNow record based on a dynamic template defined in the catalog item.
 * 
 * Usage: This script is utilized within the ServiceNow instance in the context of a business rule.
 *      Example record:
 *         Name: Autofill Description
 *         Table: sys_script
 * 
 * Author: Konner Lester
 * Date Created: 05/13/2025
 * Last Modified: 05/13/2025
 * 
 */
(function executeRule(current, previous) {
  var template = current.request_item.cat_item.u_dynamic_description_template;

  if (template) {
      // Check for Conditional logic
      var conswMatch = template.match(/CONSW\.(\w+)\((.*?)\)/);
      if (conswMatch) {
          var conswField = conswMatch[1];
          var conswConditions = conswMatch[2]; 

          // Get the value of the Conditional field
          var conswValue = current.variables[conswField] ? current.variables[conswField].toString() : "";

          // Parse the conditions
          var conditionMap = {};
          conswConditions.split(",").forEach(function(condition) {
              var [value, templateId] = condition.split(":");
              conditionMap[value] = templateId;
          });

          // Determine the template to use based on the Conditional value
          var selectedTemplateId = conditionMap[conswValue];
          if (selectedTemplateId) {
              // Extract the corresponding template
              var templateRegex = new RegExp(`^${selectedTemplateId}\\)([\\s\\S]*?)(?=\\d\\)|\\$\\))`, 'm');
              var selectedTemplateMatch = template.match(templateRegex);
              if (selectedTemplateMatch) {
                  template = selectedTemplateMatch[1].trim(); // Use the selected template
              }
          } else {
              template = ""; // No matching template, fallback to empty
          }
      }

      // Replace variables in the template
      var description = template.replace(/(VAR|REF)\.(\w+)(?:\((\w+)\))?/g, function(match, type, variableName, tableName) {
          // Get the value of the variable from current.variables
          var variableValue = current.variables[variableName];

          if (type === "VAR") {
              // Handle regular variables
              return variableValue !== undefined ? variableValue : ""; // Replace with value or empty string if undefined
          } else if (type === "REF" && tableName) {
              // Handle reference variables
              if (variableValue) {
                  var gr = new GlideRecord(tableName);
                  gr.addQuery("sys_id", variableValue);
                  gr.query();
                  if (gr.next()) {
                      return gr.getDisplayValue(); // Return the display value of the referenced record
                  }
              }
              return ""; // Replace with an empty string if the reference is invalid
          }

          return ""; // Default to an empty string for unmatched cases
      });

      // Set the description field
      current.description = description;
  } else {
      current.description = current.variables.Description; // Fallback value
  }
})(current, previous);
Last updated on