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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can use fix script for this to set for older records

1) use try catch block to handle exception

2) wrap the code inside function

updateRecords();

function updateRecords(){
	try{
		var scTask = new GlideRecord('sc_task');
		scTask.addEncodedQuery('u_userISEMPTY');
		scTask.query();
		while(scTask.next())
		{
			scTask.u_user = scTask.request_item.request.requested_for;
			scTask.update();
		}
	}
	catch(ex){
		gs.info(ex);
	}
}

Regards
Ankur

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

Hi Ankur,

I tried your fix script but no luck SC TASKS are not updated with end user value. Any alternative?

 

Thanks,

Ben.