Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Unable to apply change template through script on standard change, please suggest.

Mukesh Singh
Tera Contributor

I have created a business rule on table(x_turbo_turbonomic_turbonomic_action_approval), that creates a standard change whenever any record inserted on this table. The issue i am facing is that when i am trying to apply change template on the change record, it doesn't work.

Please suggest.

(function executeRule(current, previous /*null when async*/ ) {

    var chg_req = new GlideRecord('change_request');
    chg_req.newRecord();
    chg_req.type = 'standard';
    // chg_req.get(current.change_request_id);
    chg_req.cmdb_ci = current.target_entity_id.sn_entity_id;
    chg_req.start_date = gs.daysAgo(-1);
    // var EndDate = chg_req.start_date.addSeconds(120 * 60);
    chg_req.end_date = gs.daysAgo(-2);
    chg_req.u_environment = "QA";
    chg_req.implementation_plan = "Optimize the VM automatically by the Turbonomic as per its analysis and recommendation";
    chg_req.backout_plan = "Change back manually";
    chg_req.u_cmdb_update_required = "no";
    // TODO: Set additional change requests fields, as required
    // Use valid sys id of a standard change producer version record
    var chgProducerVersion = "ab8b184a1bc4dc58d8851f4ead4bcb35";
    var chgTemplate = new GlideRecord("std_change_record_producer");
    chgTemplate.get("sys_id", chgProducerVersion);
    var template = GlideTemplate.get(chgTemplate.name);
    chg_req.applyTemplate(template);
  //Override template values with below
    chg_req.category = chg_req.cmdb_ci.category;
    chg_req.u_subcategory = chg_req.cmdb_ci.subcategory;
    chg_req.short_description = current.description;
    chg_req.description = current.description;
    chg_req.insert();
   //Link approval entry record to newly created change record 
    gs.addInfoMessage(chg_req.sys_id);
    current.change_request_id = chg_req.sys_id;
    current.update();

})(current, previous);

1 ACCEPTED SOLUTION

Musab Rasheed
Tera Sage

Hello,

Without going in depth, this is the code to apply template. Mark my answer as correct if that helps

var chg = new GlideRecord('change_request');
chg.initialize();
chg.type = 'standard';
chg.std_change_producer_version = 'sys_id'; // Sys ID of the Standard Change Templates version
chg.applyTemplate('template_name');
chg.insert();
Please hit like and mark my response as correct if that helps
Regards,
Musab

View solution in original post

2 REPLIES 2

Musab Rasheed
Tera Sage

Hello,

Without going in depth, this is the code to apply template. Mark my answer as correct if that helps

var chg = new GlideRecord('change_request');
chg.initialize();
chg.type = 'standard';
chg.std_change_producer_version = 'sys_id'; // Sys ID of the Standard Change Templates version
chg.applyTemplate('template_name');
chg.insert();
Please hit like and mark my response as correct if that helps
Regards,
Musab

Hey Musab,

I am getting a cross-scope/fencing issue when executing the .applyTemplate() function from my custom scope. 

Do you know if there is a way to call this function from a non-global scope?