Help with a script to set assigned to based on assignment group changing and Location

carlh
Kilo Guru

Hi All,

I was asked to make a pretty simple business rule but it turns out I need to script in order to get it to work.

Could anyone help me with the following?   (Don't worry, we just hired a resource and I will be sharing this as an example so we can write our own soon)

I have added a field to our LOCATIONS table (cmn_location) called "Stock Control".   It's a reference field to users.   The plan is to use the field to assign hardware requests when the Assignment group is changed to "Stock Control".

Here's the pseudo script on the REQUEST TABLE.

Before

Insert/Update

IF 'Assignment Group' changes to "ITSM - StockRoom", Set the "Assigned to" to Same as 'location.u_stock_control_ref'

Please help if you can.

Thank you,

Carl

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Carl,



Create the Before business rule with filter condition i.e assignment group | changes to | ITSM - StockRoom with scripts as



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




// Add your code here


current.assigned_to = current.location.u_stock_control_ref;




})(current, previous);


View solution in original post

9 REPLIES 9

This is working but the asset manager asked if I could clear the assigned to when it changes back.



So if Assignment Group is Stock Room, and it changes to Purchasing, I want the assigned to be cleared


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Yes, you can do this as



if(current.assignment_group.changesTo('ITSM - StockRoom')) //Assuming ITSM - StockRoom is the group name


{


current.assigned_to = current.location.u_stock_control_ref;


}


else


{


current.assigned_to = '';


}


This wouldn't be specific to logs changing from "ITSM - StockRoom" to *something else*.



The 'assigned_to' will be cleared, if the assignment group changes, on all records in the table that has this business rule.


You are right Lee. We need to define condition filter to trigger this BR only when Assignment group | Changes to | ITSM- StockRoom OR Assignment group | Changes From | ITSM_ StockRoom.


Thanks to all who replied.   Pradeep was able to send me something I could use right away so he get's the correct answer!