Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

💡 Solved! Applying Templates (using sys_id) to Any Record Using Flow Designer Custom Actions

Amar Kutlaria
Mega Guru

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 NameData TypeRequiredDescription
    Target RecordRecord/StringYesThe record object (e.g., from a trigger) we want to update.
    Table NameStringYesThe table the record belongs to (e.g., incident).
    Template IDStringYesThe 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.

  1. Click the + button under Action Steps and select Script.

  2. In the Input Variables section of the Script Step, map your Action Inputs so the script can access them:

Script Input NameMap to Action Input
record_sysidTarget Record -> Sys ID
table_nameTable Name
template_idTemplate 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

  1. Create a Flow triggered by Service Catalog (or a simple Record Created trigger on sc_req_item).

  2. Add an Action step and search for your newly published action: Apply Template to Record.

  3. 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! 

 

1 REPLY 1

TroySasso
Tera Expert

GREAT WRITE UP @Amar Kutlaria!!!

 

You can streamline the create a little by doing the following!!  DON'T forget your INPUT Variable on the Script step!!! 🙂

 

Steps to Apply Template via Flow Designer
  1. Locate the Template: Find the sys_id of the Demand Task template (sys_template table) you want to apply.
  2. Create Action in Flow Designer: Create a new action (e.g., "Apply Demand Template").
  3. Add Script Step: Add a script step to the action with the following code:
    javascript
     
    (function execute(inputs, outputs) {
        var templateId = 'YOUR_TEMPLATE_SYS_ID'; // Replace with the actual sys_id
        var record = new GlideRecord('dmn_demand_task'); // or relevant task table
        if (record.get(inputs.recordId)) {
            var template = new GlideTemplate.get(templateId);
            template.apply(record);
            record.update();
        }
    })(inputs, outputs);
  4. Define Inputs: Create an input variable (e.g., recordId) to pass the target task's sys_id to the script.
  5. Use Action in Flow: Add this custom action after the "Create Record" step in your flow.