Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

add caller of the incident to the group being selected in the assignment group field on insert

spectura77
Tera Contributor

I have created a before business rule and  ,insert as true and used the following script ,but i am not getting the results,please adivce 

 

(function executeRule(current, previous /*null when async*/ ) {
if (current.operation() == 'insert') {
var assignmentGroup = current.assignment_group;
var caller = current.caller;
if (assignmentGroup && caller) {
var group = new GlideRecord('sys_user_group');
if (group.get(assignmentGroup)) {
group.addMember(caller);
}
}
}
})(current, previous);

2 ACCEPTED SOLUTIONS

Vishal Birajdar
Giga Sage

Hi @spectura77 

 

Can you try below code :

 

 

/*1. Get values from current Incident form */
var caller = current.getValue('caller_id');
var group  = current.getValue('assignment_group');

/*2. glide record on sys_user_grmember table */
var grMem = new GlideRecord('sys_user_grmember');
grMem.addEncodedQuery('group=' + group +'^user=' + caller);
grMem.query();
if(!grMem.next()){

/*3. Add user to group */   
var grAddMem = new GlideRecord('sys_user_grmember');
grAddMem.initialize();
grAddMem.setValue('group',group);   //group
grAddMem.setValue('user',caller);  //caller
grAddMem.insert();

} else {
   gs.log("User already a member of group"); 
}

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

Hello @spectura77 ,

The script is correct. It worked in my PDI. Try to test it again with creating a new record. It should work.

if it doesn't work then please refer to @Vishal Birajdar script that is also another way to achieve the requirement.

Thanks,

Alka

View solution in original post

8 REPLIES 8

Hello @spectura77 ,

The script is correct. It worked in my PDI. Try to test it again with creating a new record. It should work.

if it doesn't work then please refer to @Vishal Birajdar script that is also another way to achieve the requirement.

Thanks,

Alka

Hi @Alka_Chaudhary ,this script worked there was another business rule that was overriding the business rule that i created 

thanks 

Vishal Birajdar
Giga Sage

Hi @spectura77 

 

Can you try below code :

 

 

/*1. Get values from current Incident form */
var caller = current.getValue('caller_id');
var group  = current.getValue('assignment_group');

/*2. glide record on sys_user_grmember table */
var grMem = new GlideRecord('sys_user_grmember');
grMem.addEncodedQuery('group=' + group +'^user=' + caller);
grMem.query();
if(!grMem.next()){

/*3. Add user to group */   
var grAddMem = new GlideRecord('sys_user_grmember');
grAddMem.initialize();
grAddMem.setValue('group',group);   //group
grAddMem.setValue('user',caller);  //caller
grAddMem.insert();

} else {
   gs.log("User already a member of group"); 
}

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Hi @Vishal Birajdar  this script worked 

Thanks