elHow to copy catalog items variables to requested items custom field?

Enkhdalai
Tera Expert

Hello Everyone, 

 

I know there is a lot of articles on copying variables data to custom field in requested item. I have tried one or two script and none of them is working. 

 

What I am trying to do is when someone requests item from service catalog, they fill user reference field (new_emp_name)  in variable and I need to copy that data to requested items reference field (u_new_employee). 

Variable name: new_emp_name

Field name: u_new_employee

 

Busines rule condition: sc_req_item table

Insert, Update

 

Item is New employee request

 

 

Business rule script:

 

(function executeRule(current, previous /*null when async*/) {

var gr = new GlideRecord('sc_req_item');
gr.addQuery('request',current.sys_id);
gr.query();
while(gr.next())


{

gr.u_new_employee = current.variables.new_emp_name ;


gr.update();


}

 

})(current, previous);

 

Can anyone help me on this? 

 

Thanks.

1 ACCEPTED SOLUTION

Sebastian L
Mega Sage

Okay first of all, never do ".update()" in a business rule on the same table, as it will make it run twice or sometimes in a loop. If general you should never have to write a business rule on the catalog tables, but seems like you need to do it, so I'll help you. 

 

Secondly, you should change your business rule to run on "before" and only on "insert". And have the item in the condition still. 

Then adjust your script accordingly: 

 

(function executeRule(current, previous /*null when async*/) {
if(current.variables.new_emp_name) {
current.u_new_employee = current.variables.new_emp_name;
}
})(current, previous);

 

 


Best regards,
Sebastian Laursen

View solution in original post

2 REPLIES 2

Sebastian L
Mega Sage

Okay first of all, never do ".update()" in a business rule on the same table, as it will make it run twice or sometimes in a loop. If general you should never have to write a business rule on the catalog tables, but seems like you need to do it, so I'll help you. 

 

Secondly, you should change your business rule to run on "before" and only on "insert". And have the item in the condition still. 

Then adjust your script accordingly: 

 

(function executeRule(current, previous /*null when async*/) {
if(current.variables.new_emp_name) {
current.u_new_employee = current.variables.new_emp_name;
}
})(current, previous);

 

 


Best regards,
Sebastian Laursen

Thanks, Sebastian. This script works. Have a nice day.