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.

Business rule help to copy watch list from RITM to Task

Josh Hines
Tera Contributor

Hello,

 

I am looking for a way to pull the watch list from a new RITM into the SCTask record. I have set up a business rule to run after the Watch List changes, but am having issues getting my code to work. I would appreciate any help on getting this to work from you smart people. Thank you!

 

JoshHines_0-1696617463802.png

(function() { 

   //get the gliderecord for the parent request

   var task = new GlideRecord('sc_task'); 

   if (task.get(current.catalog_task)) { 

           task.watch_list = current.watch_list;

     task.update(); 

   }


})();

 

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Hi Josh,

Since this BR is running on the sc_req_item table, 'current' refers to the RITM record that is being changed.  There is not a field on the sc_req_item table named catalog_task, so that's where your script is failing.  Something like this should work, copying the watch_list to ALL Catalog tasks that exist at the time of the update:

var task = new GlideRecord('sc_task'); 
task.addQuery('parent', current.sys_id);
task.query();
while (task.next()) {
     task.watch_list = current.watch_list;
    task.update(); 
}

View solution in original post

6 REPLIES 6

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

Now depending on your requirement, if you want to copy the watchlist values when a sc_task gets created then write a before insert business rule on sc_task table with the below code:-

 

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

current.watch_list=request_item.watch_list;

})(current, previous);

 

Now if you want to update the watchlist on sc_task on change of watchlist value the create a after update business rule on sc_req_item table and use the below code:-

 

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

 var cat_task = new GlideRecord('sc_task'); 

   if (cat_task.get(current.sys_id)) { 

 cat_task.watch_list = current.watch_list;
 cat_task.update(); 

   }

})(current, previous);

 

Please mark my answer as correct based on Impact.

Hello @Josh Hines 

 

If the requirement is to update it always on change of watchlist then what @Brad Bowman  has explained is correct I missed the current part while typing the script. 

 

Please follow @Brad Bowman  answer.

 

Thanks.

Brad Bowman
Kilo Patron
Kilo Patron

Hi Josh,

Since this BR is running on the sc_req_item table, 'current' refers to the RITM record that is being changed.  There is not a field on the sc_req_item table named catalog_task, so that's where your script is failing.  Something like this should work, copying the watch_list to ALL Catalog tasks that exist at the time of the update:

var task = new GlideRecord('sc_task'); 
task.addQuery('parent', current.sys_id);
task.query();
while (task.next()) {
     task.watch_list = current.watch_list;
    task.update(); 
}

Thank you! That worked.