- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2022 02:47 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2022 03:05 AM - edited 11-14-2022 03:06 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2022 03:05 AM - edited 11-14-2022 03:06 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-14-2022 04:59 PM
Thanks, Sebastian. This script works. Have a nice day.