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

Please let me know

Now we can run a small test

var a = "u_end_user=NULL";
var count1 = 0;
var sc = new GlideRecord('sc_task');
sc.addEncodedQuery(a);

sc.setLimit(10);
sc.query();
while(sc.next())
{
gs.print("Parent Sys_id: "+sc.parent.requested_for);
count1++;
}
gs.print(count1);

Are you able to see 10 sys_ids?

Regards
Abhinay

Hello Ben,

var a = "u_end_user=NULL";

var b = "parent.ref_sc_req_item.requested_forISNOTEMPTY";

var count1 = 0;
var sc = new GlideRecord('sc_task');
sc.addEncodedQuery(a);
sc.addEncodedQuery(b);
sc.setLimit(10);
sc.query();
while(sc.next())
{
gs.print("Parent Sys_id: "+sc.parent.ref_sc_req_item.requested_for);
count1++;
}
gs.print(count1);

I hope this will work, only if requested_for field is not modified

Please share the output if possible

Regards,
Abhinay Singh

Ben42
Tera Contributor

Abhinay,

Now I see 10 sys ids.

*** Script: Parent Sys_id: cba77605XXXXXXXXX2cc309e7c9619bc
*** Script: Parent Sys_id: eb883XXXXXXXXX407a2c9c94db9619c1
*** Script: Parent Sys_id: bba74605dbXXXXXXXXe7c9619bcd54f6
*** Script: Parent Sys_id: eXXXXXXXX4rf5640157f5a78dc9619d5
*** Script: Parent Sys_id: bba74605db7be70482ccXXXXXXXXf54f
*** Script: Parent Sys_id: eb883a9edbXXXXXXXXc94db9619c1d3f
*** Script: Parent Sys_id: bba74605dXXXXXXXX09e7c9619bc5rf6
*** Script: Parent Sys_id: eXXXXXXXXf56tt40157f5a78dc9619d5
*** Script: Parent Sys_id: fb8XXXXXXXXc9c94db961913c5rft6yh
*** Script: Parent Sys_id: ebe255fedb156b40157fXXXXXXX619d5
*** Script: 10

Thanks,

Ben.

Great News, now lets try to actually update 10 records

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();
}

Please let me know if atleast 10 records gets updated

Regards,
Abhinay Singh

Ben42
Tera Contributor

Abhinay,

I get output like below. But, u_end_user field on catalog task is still empty for all records.

find_real_file.pngThanks,

Ben.