I need a way to apply a template to change record in background with template sys_id rather than template Name

sanju41
Giga Contributor

Example :   var ga= new GlideRecord("change_request");

                                  ga.applyTemplate("Name of template") ;

                                  ga.insert();

            I need a way to apply template with sys_id of template rather than template name

1 ACCEPTED SOLUTION

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

It's undocumented, but I have seen this code in Event Managemt business rule where you can apply a template via SysId using the following syntax:


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



Set the Sys_ID in the first parameter and the record you want to apply it to in the apply function. This has saved me a headache or two in the recent past.



Please mark helpful or correct if applicable.


View solution in original post

5 REPLIES 5

Geoffrey2
ServiceNow Employee
ServiceNow Employee

http://wiki.servicenow.com/index.php?title=GlideRecord#applyTemplate&gsc.tab=0



The function only takes a name.   But you can get the name via the sys_id.



var templateID = '3d5fa670dbdd2a00bd3cff9dbf96195c';


var templateName = '';


var template = new GlideRecord('sys_template');


if (template.get(templateID)) {


    templateName = template.name;


}



var chg = new GlideRecord("change_request");


chg.applyTemplate(templateName) ;


chg.insert();



I should also point out that hard-coding sys_ids is considered bad-practice and will be flagged in an ACE report.


Michael Fry1
Kilo Patron

Are you applying the Template to every change? If so you can modify the module to include the template.


No, i need to apply a template in Workflow for one Service request which create's a create record


sanju41
Giga Contributor

*Change record