We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

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

VishalB06557037
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 @VishalB06557037 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 @VishalB06557037 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 

VishalB06557037
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 @VishalB06557037  this script worked 

Thanks