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

Great, thank you for sharing. From this we can confirm that the query is working.
Now we can run a small update 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

Great, thank you for sharing. From this we can confirm that the query is working.
Now we can run a small update 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

Ben42
Tera Contributor

No, I see count as 10 but sys ids as empty like below.

*** Script: Parent Sys_id: 
*** Script: Parent Sys_id:
*** Script: Parent Sys_id:
*** Script: Parent Sys_id:
*** Script: Parent Sys_id:
*** Script: Parent Sys_id:
*** Script: Parent Sys_id:
*** Script: Parent Sys_id:
*** Script: Parent Sys_id:
*** Script: Parent Sys_id:
*** Script: 10

Thanks,
Ben.

Ben42
Tera Contributor

Hi Abhinay,

I am running in background script and I have admin access. And, for most of the suggested scripts the transaction is taking too long to execute but catalog task records are not being updated.

The below script executes quickly but records are not being updated.

updatetasks();

function updatetasks()
{
var sc = new GlideRecord('sc_task');
while(sc.next())
{
sc.u_end_user =sc.getValue('request_item.requested_for');
sc.update();
}
}

 

Thanks,

Ben.

Thank you for sharing.

Now, please run below script in background and let me know what response do you get

 

var a = "parent.numberSTARTSWITHRITM^u_end_user=NULL";
var count1 = 0;
var sc = new GlideRecord('sc_task');
sc.addEncodedQuery(a);
sc.query();
while(sc.next())
{
count1++;

}
gs.print(count1);

 

Please advise