{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":39231,"path":"platform-administration/r_ScriptedTemplates.md","title":"Scripted templates","bundle":null,"doc_type":null,"release":"australia","body":"\n# Scripted templates\n\nYou can apply an active template to a record using JavaScript.\n\n## Apply a template to current\n\nTo apply a template, use the applyTemplate method.\n\n```\ncurrent.applyTemplate(\"<templatename>\");\n```\n\n## Apply a template to a GlideRecord\n\nTo 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.\n\n```\nvar rec1 = new GlideRecord(\"incident\");\nrec1.initialize();\nrec1.applyTemplate(\"my_incident_template\");\n```\n\n## Apply a template from a UI action\n\nThe 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.\n\n```\nvar change = new GlideRecord(\"change_request\");\nchange.initialize();\nchange.short_description = current.short_description;\nchange.description = current.u_details;\nchange.cmdb_ci = current.u_service;\nchange.priority = current.priority;\nchange.requested_by = current.caller_id;\nchange.assignment_group.setDisplayValue('Change & Release');\nchange.u_status = 'New';\nchange.parent = current.number;\nchange.applyTemplate(\"standard_rfc\");\ncurrent.rfc = change.insert();\ncurrent.comments = 'Change ' + change.number + ' created.';\n\nvar mySysID = current.update();\n\ngs.addInfoMessage(\"Change \" + change.number + \" created\");\naction.setRedirectURL(change);\naction.setReturnURL(current);\n```\n\n## Script a template with child templates\n\nWhen 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.\n\nFor 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.\n\n## Apply a template using a sys\\_id instead of a name\n\nTo apply a template using a sys\\_id, use the following method.\n\n```\nGlideTemplate.get(template.sys_id).apply(GlideRecord)\n```\n\n**Parent Topic:**[Using form templates](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/platform-administration/c_Templates.md)\n\n**Related topics**  \n\n\n[Create a template using the Template form]()\n\n[Create templates for related task records]()\n\n[Create a template by saving a form]()\n\n[Schedule record creation from a template]()\n\n[Create a module for a template]()\n\n[Template bar]()\n\n[Toggle the template bar]()\n\n"}}