💡 Solved! Applying Templates (using sys_id) to Any Record Using Flow Designer Custom Actions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
I recently ran into a wall trying to automate a standard process: applying a pre-defined ServiceNow Template to a record inside a Flow Designer flow. While Flow Designer is powerful, there's no native "Apply Template" action.
I spent time searching, but the information was scattered. After some experimentation, I figured out the cleanest, most reusable solution: a Custom Action with a Script Step.
I'm sharing this step-by-step guide to save you the headache I went through. Let’s make this common automation challenge a thing of the past!
🛠️ Step 1: Building the Reusable Custom Action
We need to create a function that takes three inputs—the record, the table, and the template name—and applies the template.
Navigate to Flow Designer and select New > Action.
Set the properties:
Action Name: Apply Template to Record
Description: Applies a specified sys_template to a target GlideRecord using a Script Step.
Click Inputs and define the following variables. These are the pieces of data your flow will provide to the action:
Input Name Data Type Required Description Target Record Record/String Yes The record object (e.g., from a trigger) we want to update. Table Name String Yes The table the record belongs to (e.g., incident). Template ID String Yes The exact sys_id of the sys_template to use.
Step 2: The Core Logic – Scripting the applyTemplate() Method
The true power comes from server-side JavaScript using the GlideRecord API. We'll use the applyTemplate() method, which is specifically designed for this task.
Click the + button under Action Steps and select Script.
In the Input Variables section of the Script Step, map your Action Inputs so the script can access them:
| Script Input Name | Map to Action Input |
| record_sysid | Target Record -> Sys ID |
| table_name | Table Name |
| template_id | Template ID |
(function execute(inputs, outputs) {
gs.info('Apply Template Action: Starting script for record ' + inputs.record_sysid + ' on table ' + inputs.table_name);
// 1. Query the target table and find the specific record
var targetGR = new GlideRecord(inputs.table_name);
if (!targetGR.get(inputs.record_sysid)) {
gs.error('Apply Template Action Failed: Target record ' + inputs.record_sysid + ' not found on table ' + i nputs.table_name);
return; // Exit the script if the record isn't found
}
// 2. Get the template object using GlideTemplate utility class
var templateObj = GlideTemplate.get(inputs.template_id));
if (templateObj) {
gs.info('Apply Template Action: Template object retrieved successfully. Applying values...');
// 3. Apply the template to the retrieved GlideRecord object
templateObj.apply(targetGR);
// 4. IMPORTANT STEP: Update the target record for the template changes to execute and be saved
targetGR.update();
gs.info('Apply Template Action Success: Template "' + inputs.template_id + '" applied and record updated for ' + inputs.record_sysid);
} else {
gs.error('Apply Template Action Failed: GlideTemplate.get(' + templateID + ') returned null.');
}
})(inputs, outputs);
🔑 Why this Works (The Logic):
Template Retrieval: We use the specialized GlideTemplate.get(sys_id) method, to load the template object directly from the database using the unique ID provided by the flow.
Template Application: The templateObj.apply(targetGR) method executes the template's logic, populating fields onto the targetGR object in memory.
Committing Changes: The targetGR.update() call is absolutely mandatory to save the changes to the database.
Logging: The gs.info() and gs.error() statements provide information for monitoring and troubleshooting in the System Logs.
3. Save and Publish your Custom Action.
🏃 Step 3: Using the Action in a Flow
Now, whenever you need to apply a template in a flow, you can use this single, clean action!
Example: Auto-Populating Fields for a Specific Catalog Item
Create a Flow triggered by Service Catalog (or a simple Record Created trigger on sc_req_item).
Add an Action step and search for your newly published action: Apply Template to Record.
Map the inputs using the data pills from your trigger:
Target Record: Drag the Requested Item Record data pill.
Table Name: Type the static string: sc_req_item
Template ID: Type the static string for your template (e.g., sys_id).
The flow will now automatically find that template, apply the fields (like Priority or Assignment Group), and save the record—all without complicated lookups or scripting inside the flow itself!
🤝 Working Together for Better Solutions
This method offers ultimate flexibility and code reuse. I hope this helps anyone else who was stuck on this! Feel free to drop a comment if you find other great ways to use this action!
