The CreatorCon Call for Content is officially open! Get started here.

GlideTemplate not working as expected

Sara_M
Tera Contributor

Hi All, I am facing the below issue:

I have standard change template which has change task template associated with it.

I am trying to create the standard change by calling that template via scripting from both BR and workflow. But from both of these places, when i trigger below GlideTemplate code, only standard change is getting created. Standard change task is not getting created.

But the below script is working perfecty from background scripts.

I found it wierd. Can you please help me here!

 

var producerID = 'xxxxxxxxxxxxxxxxxxxxx';
var templateVersion = new GlideRecord('std_change_producer_version');
templateVersion.addQuery('std_change_producer=' + producerID + '^std_change_proposal.state=3');
templateVersion.orderByDesc('version');
templateVersion.query();
templateVersion.next();

var chg = new GlideRecord('change_request');
chg.initialize();
chg.type = 'standard-new';
chg.state = '-3'; //draft
chg.work_notes = 'Producer version: ' + templateVersion.getUniqueValue();
chg.std_change_producer_version = templateVersion.getUniqueValue(); // Sys ID of the Standard Change Templates version
gs.print(chg.std_change_producer_version);
var template = GlideTemplate.get(chg.std_change_producer_version.std_change_producer.template);
template.apply(chg);
var x = chg.sys_id;
chg.insert();

 

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Sara_M 

when you use background script the transaction context is same and there may not be conflict with the OOTB Business rule which creates change task.

When you run BR or workflow the operation may or may not be synchronous.

This OOTB Business rule creates Standard Change Tasks

AnkurBawiskar_0-1751007580635.png

 

My Recommendations:

1) I will suggest let system handle the change task creation after your change is created.

OR

2) Another way is to suppress the OOTB business rule and handle the task creation from your BR or workflow script itself

Try this script

var producerID = 'xxxxxxxxxxxxxxxxxxxxx';
var templateVersion = new GlideRecord('std_change_producer_version');
templateVersion.addQuery('std_change_producer=' + producerID + '^std_change_proposal.state=3');
templateVersion.orderByDesc('version');
templateVersion.query();
templateVersion.next();

var chg = new GlideRecord('change_request');
chg.initialize();
chg.type = 'standard-new';
chg.state = '-3'; //draft
chg.work_notes = 'Producer version: ' + templateVersion.getUniqueValue();
chg.std_change_producer_version = templateVersion.getUniqueValue(); // Sys ID of the Standard Change Templates version
gs.print(chg.std_change_producer_version);
var template = GlideTemplate.get(chg.std_change_producer_version.std_change_producer.template);
template.apply(chg);
var x = chg.sys_id;
chg.setWorkflow(false); // avoid business rule
chg.insert();

// call the task creation explicitly
new StdChangeUtils().createChangeTasks(chg);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Sara_M
Tera Contributor

Hi Ankur,

Many Thanks for taking your time adn providing the suggestion. I have found the root cause for this. Change tasks were getting created in another domain because of one custom BR written on global domain. We have fixed this now.