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

Alka_Chaudhary
Mega Sage
Mega Sage

Hello @spectura77 ,

You can create a on insert business rule.

To add user in group, you have to glide 'sys_user_grmember' table.  Also, have to check if user already exist in the group don't add them. 

Please try the below script:

 

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

    // Add your code here
    var flag = false;
    var caller = current.caller_id;
    var group = current.assignment_group;
    var gr = new GlideRecord('sys_user_grmember');
    gr.addQuery('group', group);
    gr.query();
    while (gr.next()) {
        if (gr.user == caller) {
            flag = true;
        }
    }

    if (flag == false) {
        gr.initialize();
        gr.user = caller;
        gr.group = group;
        gr.insert();
    }
})(current, previous);

 

 

If this response clears up your doubt, kindly flag it as both helpful and correct.

Thanks,

Alka

 

Hi @Alka_Chaudhary thank you for the response , I have tried this script but still not getting the result 

Hello @spectura77 ,

On which table you are creating the business rule and what is the when to run condition?

Please share the screenshot of the script that you wrote.

Hi @Alka_Chaudhary screen shot of the business rule and the script 

Thanks 

spectura77_0-1697792317110.pngspectura77_1-1697792365480.png