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

leebooth
Kilo Expert

Hi Carl,



I think I understand what you are asking for - Here's how I would go about it.


Create your business rule with advanced box ticked. In the "When to run" tab choose when assignment group changes.


find_real_file.png



Then in your Advanced tab, put a script condition to only run the script when the assignment group is now "ITSM - StockRoom"


find_real_file.png


The script will find the current location (which i'm guessing is a reference field) in the 'cmn_location' table & then set your 'assigned to' to be the user stored in your new field on the location record.




Here the code for copy & pasting purposes!



Condition:


(current.assignment_group == "ITSM - StockRoom")


Script:


var gr = new GlideRecord('cmn_location');


gr.addQuery('sys_id', current.location);


gr.query();



if(gr.next()) {


  current.assigned_to = gr.u_stock_control_ref;


}


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);


Ah yes, you could just dot walk to it instead of creating a glide record!



Do this!


This is close.



But I need it to be the Location from the incident which is referenced by the 'incidentid' field on Request.. Can I triple dot walk?



I tried this but not working.



current.assigned_to = current.incidentid.location.u_stock_control_ref;