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

Stewe Lundin
Mega Guru

You already have the data in the sc_task. why do you need a custom field 

sc_task.request_item.requested_for

So why a Custom field ?  


Hello Ben,

If the custom field is a Reference field of user's table, than you can use below code

var gr= new GlideRecord('sc_task');
gr.addNullQuery('custom_field_name');

gr.query();

while(gr.next()) {
gr.custom_field_name = gr.request_item.requested_for;
gr.update();
}

Else if it is a string field, use below

var gr= new GlideRecord('sc_task');
gr.addNullQuery('custom_field_name');

gr.query();

while(gr.next()) {
gr.custom_field_name = gr.request_item.requested_for.getdisplayvalue();
gr.update();
}

 

Please let me know if this works

Hello,

End users report on TASK table so, for consistency and to avoid confusion to end users, we have created a custom user field to copy fields on requested_for, requested_by, afftected_user, etc..

 

Thanks,

Ben.

 

Abhinay1
Giga Expert

Hello Ben,

If the custom field is a Reference field of user's table, than you can use below code

var gr= new GlideRecord('sc_task');
gr.addNullQuery('custom_field_name');

gr.query();

while(gr.next()) {
gr.custom_field_name = gr.request_item.requested_for;
gr.update();
}

Else if it is a string field, use below

var gr= new GlideRecord('sc_task');
gr.addNullQuery('custom_field_name');

gr.query();

while(gr.next()) {
gr.custom_field_name = gr.request_item.requested_for.getdisplayvalue();
gr.update();
}

 

Please let me know if this works