Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Business rule to add user to group

Craig Nicholls
Tera Contributor

Hello Guys,

 

I have created a true/false field on the Customer_Contact table labelled "u_non_standard_request_authorised" I am looking for a way to add the user of the record to a group I have created "Non-Standard Request Authorised" when the value is set to true. 

 

I am assuming a business rule is the best route here but not sure where to begin. I assume a rule that monitors Customer_Contact records and triggers when the condition "u_non_standard_request_authorised" is set to true with an after and on insert and update. is the best solution.

 

Could someone assist with the actions I need to configure to update the group?

 

thanks

Craig

 

 

3 REPLIES 3

MAHAMKALI
Tera Contributor

Hi @Craig Nicholls 
You can use Before BR - Insert & Update 

with Condition u_non_standard_request_authorised is true

and in the script you can the following script

 

 

var gr = new GlideRecord('sys_user_grmember');
gr.initialize();
gr.group=current.assignment_group;
//field name or the object where group sys_id is stored on the form
gr.user = current.assigned_to;
//field name or the object where user sys_id is stored on the form.
gr.insert();

 

 

Hope this will work.

Vigneshwara Red
Tera Contributor

Hi,
I think it can be achieved in different ways:

1) Writing the code in script include and calling the same in client script on change of the checkbox and is true create a record in sys_user_grmember table.

2) BR will work as well on the check box changes and is set to true condition and insert a record in sys_user_grmember table.

 

Thanks,

Hope this helps..!!


2) 

Steven Parker
Giga Sage

So create an After Business Rule against the Customer_Contact table.  On the "When to Run" tab in the Filter Conditions section, add a condition for "Non Standard Request Authorized" is true (or changes to true).  

 

Then on the Advanced tab you would add this script:

 

 

 

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

var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', current.sys_id);
gr.addQuery('group', 'sys_id_of_group');//sys_id of Non-Standard Request Authorised
gr.query();
if(!gr.next())
   {
   gr.initialize();
   gr.user = current.sys_id;
   gr.group = 'sys_id_of_group';//sys_id of Non-Standard Request Authorised
   gr.insert();
}

})(current, previous);

 

 

 

This will add the current user record on the Customer_Contact table to the group when that field changes to True.  Assuming that record on the Customer_Contact table is a user in the system.


Please mark this response as correct and/or helpful if it assisted you with your question.
Steven