---
sourceDocument: Xanadu ServiceNow AI Platform Administration
sourceDocumentLink: https://servicenow-prod.fluidtopics.net/r/xanadu/platform-administration

 Release :

    - xanadu

ft:locale :

    - en-US

ft:publication_title :

    - Xanadu ServiceNow AI Platform Administration

ft:clusterId :

    - platadm

bundleId :

    - platadm

workflow :

    - Platform


---

# Scripted templates

# Scripted templates {#ariaid-title1}

* Release version: Xanadu
* 
* Updated August 1, 2024
* 
* ![](https://www.servicenow.com/docs/portal-asset/ico-clock) 1 minute to read

You can apply an active template to a record using JavaScript.

## Apply a template to current

To apply a template, use the applyTemplate method.

    current.applyTemplate("<templatename>");

## Apply a template to a GlideRecord

To apply the template to a record other than current, change current to a GlideRecord
variable. When using a GlideRecord variable, you may need to initialize it after declaring
the variable.

    var rec1 = new GlideRecord("incident");
    rec1.initialize();
    rec1.applyTemplate("my_incident_template");

## Apply a template from a UI action

The following script demonstrates a possible customization to the Create Change UI action on the Problem form. After you add this script to the UI action, a user can select the UI action to create a change record with information from both the problem record and the change template.

    var change = new GlideRecord("change_request");
    change.initialize();
    change.short_description = current.short_description;
    change.description = current.u_details;
    change.cmdb_ci = current.u_service;
    change.priority = current.priority;
    change.requested_by = current.caller_id;
    change.assignment_group.setDisplayValue('Change & Release');
    change.u_status = 'New';
    change.parent = current.number;
    if(0 == change.applyTemplate("standard_rfc")) {
        current.rfc = change.insert();
        current.comments = 'Change ' + change.number + ' created.';
    }

    var mySysID = current.update();

    gs.addInfoMessage("Change " + change.number + " created");
    action.setRedirectURL(change);
    action.setReturnURL(current);

## Script a template with child templates

When using applyTemplate with a template that has one or more child
templates, the system creates the parent record before applying the child templates. This
behavior ensures that any references or dot-walked fields from the child record to the
parent have a valid target.

For example, if a template for the Change Request table has a child template for the Change
Task table, applying the Change Request template inserts a Change Request record into the
database. It assigns this record as the Change request for the Change Task record, then
applies the child template to the Change Task record.

## Apply a template using a sys_id instead of a name {#r_ScriptedTemplates__section_wy3_2yz_ztb}

To apply a template using a sys_id, use the following method.

    GlideTemplate.get(template.sys_id).apply(GlideRecord)

**Related concepts**   

* [Template bar](https://servicenow-prod.fluidtopics.net/WrgxrzlmlxurQZ4tKh_yPg "Use the template bar to apply, edit, and create templates.")  
**Related tasks**   

* [Create a template using the Template form](https://servicenow-prod.fluidtopics.net/R1_A6UDhw4K9hZo67UFveA "Create a template record for any table to populate certain fields automatically.")
* [Create templates for related task records](https://servicenow-prod.fluidtopics.net/lcR0pnTpJbu1_64LFF0bkQ "Administrators can create a template for a Task table record that also creates one or more related records in the child Task table.")
* [Create a template by saving a form](https://servicenow-prod.fluidtopics.net/gxTRL5VR2I3uOLV9LuYABg "Save a populated form as a template.")
* [Create records based on a template](https://servicenow-prod.fluidtopics.net/6GsIVluJ_U~G8wUeBQHgZg "You can create and schedule a scheduled job to create records based on a template. For example, you can regularly create a populated task record to perform a weekly backup.")
* [Create a module for a template](https://servicenow-prod.fluidtopics.net/6J~YbXE_Ik1ct8gg5GuQtg "You can create a module to open a form with pre-populated template data.")
* [Toggle the template bar](https://servicenow-prod.fluidtopics.net/Z20zMntRChz1DDWdaTgPLA "The template bar appears at the bottom of forms. It provides shortcuts to apply, edit, and create templates.")

