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.

Copy Reference field value from RITM to ScTasks

Ben42
Tera Contributor

Hello Guys,

For all existing catalog tasks, I want to copy "request_item.requested_for" field on RITM to "user" (custom) field on ScTasks. How can I achieve this? and How does the script look like?

We have few hundreds of catalog tasks, So, I wanted to do a bulk update using script.

 

Any help is appreciated.

Thanks,

Ben.

1 ACCEPTED SOLUTION

Hey Ben,

Just a quick check, in the list view of sc_task table, group by u_end_user field and confirm if all are under empty.

Also, added a new line to the code, please try

var a = "u_end_user=NULL";
var b = "parent.ref_sc_req_item.requested_forISNOTEMPTY";

var sc = new GlideRecord('sc_task');
sc.addEncodedQuery(a);
sc.addEncodedQuery(b);
sc.setLimit(10);
sc.query();
while(sc.next())
{
sc.u_end_user = sc.parent.ref_sc_req_item.requested_for;
sc.update();
sc.setWorkflow(false);

}

Regards,
Abhinay

View solution in original post

41 REPLIES 41

Hi Mohith,

Since I want to query u_end_user is empty, I have modified second line of your script. but, when I execute the transaction never ends.. like transaction running for more than 4000 seconds and I had to cancel it.

 

I execute the below script in background scripts.

var sc = new GlideRecord('sc_task');
sc.addEncodedQuery('u_end_userISEMPTY');
sc.query();
while(sc.next())
{

gs.info('found the tasks'+sc.request_item.request.requested_for);
sc.u_end_user =sc.request_item.request.requested_for.toString();
sc.update();
}

Thanks,

Ben.

Hey Ben,

i think there are many records in sc task table .

by any chance is it for a specific catalog item where all these tasks are tagged to if yes try below script 

var sc = new GlideRecord('sc_task');
sc.addEncodedQuery('u_end_userISEMPTY');
sc.addQuery('request_item.item','<your catalog item sys_id>')
sc.query();
while(sc.next())
{

gs.info('found the tasks'+sc.request_item.request.requested_for);
sc.u_end_user =sc.request_item.request.requested_for.toString();
sc.update();
}

this add query that i have added in thrid line filters only for a specific catalog item .

Try using this it might filter un necessary tasks 

 

I have around 7000 records. and No these records are not specific to one catalog item.

 

Thanks,

Ben.

then try this and see

var sc = new GlideRecord('sc_task');
sc.addEncodedQuery('u_end_userISEMPTY');
sc.query();
while(sc.next())
{
var gr  = new GlideRecord('sc_req_item');
gr.addQuery('sys_id',cs.request_item);
gr.query();
if(gr.next())
{
sc.u_end_user =gr.request.requested_for.toString();
sc.update();
}

}

Swapnil Shirsik
Giga Guru

Hi Ben,

You can create a before update business rule on sc_task table. In the actions tab condition builder set the field values as requested for- same as- Requested item.requested for custom field. (This field will be available after you select show related fields)

Please mark this answer as correct/helpful if it has added value.