Gowrisankar Sat
Tera Guru

Scripting Standard Change Templates in a Scoped Applications!

Recent times I faced issue to Apply Standard Change Templates in Scoped Applications as applyTemplate function doesn't work in scoped application.

Here is what I did to Achieve this!

Step 1: Query from Standard Change Template Versions table with the name of your Template to make sure the latest version of Change Template is fetched.

var chgRec = new GlideRecord('change_request'); //Initialize your Change Record
chgRec.initialize();
var chgProd = new GlideRecord("std_change_producer_version");
chgProd.addQuery("std_change_producer.name",<The Name of your Template>);
chgProd.orderByDesc("version"); //latest version of Change Template is fetched
chgProd.query();
if (chgProd.next()) {
chgProp = chgProd.std_change_proposal;
} else {
return false+":Issue with Change templates";
}

Step 2: Fetch the Standard Change Proposal Record, because Template values stays on this record.

var chgPropGR = new GlideRecord("std_change_proposal");
chgPropGR.addQuery("sys_id", chgProp);
chgPropGR.query();
if (chgPropGR.next()) {
var template = chgPropGR.template_value.toString();

Step 3: Apply Template with the Magical "applyEncodedQuery" Function.

chgRec.applyEncodedQuery(chgPropGR.getValue('template_value'));//This is the Magical Statement of this Code
}
chgRec.std_change_producer_version = chgProd.sys_id;
chgRec.start_date = new GlideDateTime(<Planned Start Date>);
chgRec.end_date = new GlideDateTime(<Planned End Date>);
chgRec.insert();

That's It!
You don't need ApplyTemplate Anymore and Trust "applyEncodedQuery" for this...

You can also see the Change Tasks getting Created if there are Change Task Templates on the Standard Change Proposal Record.

HOPE THIS HELPS! STAY SAFE!! MARK HELPFUL IF YOU LIKED THIS!!!

Version history
Last update:
‎06-29-2020 02:33 AM
Updated by: