logo

NJP

Scripting Standard Change Templates in a Scoped Applications!

Import · Jun 29, 2020 · article

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 RecordchgRec.initialize();var chgProd = new GlideRecord("std_change_producer_version");chgProd.addQuery("std_change_producer.name",);chgProd.orderByDesc("version"); //latest version of Change Template is fetchedchgProd.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();chgRec.end_date = new GlideDateTime();

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!!!

Labels:

image

View original source

https://www.servicenow.com/community/itsm-articles/scripting-standard-change-templates-in-a-scoped-applications/ta-p/2299132