Script to find whether users belong to a particular group or not.

pratik_kumar
Giga Expert

Hi All,

I have a requirement in which I need to validate whether "opended_by" field user is part of a particular group or not.

Complete requirement;-

I need to create a breakdown in PA, so that we can use that breakdown to filter out opened_by users belongs to a particular group.

So i thought of writing a script for breakdown mapping.

Kindly help me on this.

3 REPLIES 3

Chuck Tomasi
Tera Patron

You can find out if the currently logged in user is the member of a particular group using isMemberOf() from the gs.getUser() method.



http://wiki.servicenow.com/index.php?title=Getting_a_User_Object#gsc.tab=0


Ankur Bawiskar
Tera Patron
Tera Patron

HI Pratik,



Client Side:


var userSysId = g_form.getValue('opended_by'); // client side i.e. client script etc


var groupName = 'CAB Approval'; // for example add your group name here



var gr = new GlideRecord('sys_user_grmember');


gr.addQuery('user', userSysId);


gr.addQuery('group.name', 'groupName');


gr.query();


if(gr.next()){


alert("User is member of group");


}


else{


alert("User is not member of group");


}



Server Side:



var userSysId   = current.opended_by;


var ourUser = gs.getUser();


ourUser = ourUser. ­getUserByID(userSysId);



var isMember = ourUser. ­isMemberOf( ­'RMA Approvers'); // add your group name here




Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

dvp
Mega Sage
Mega Sage

Hello Pratik,



Try the below script



var gr = new GlideRecord('incident')


gr.query();



while(gr.next()){




var grp = new GlideRecord('sys_user_grmember');


grp.addQuery('group', 'GROUP_SYS_ID');


grp.addQuery('user', gr.opened_by);


grp.query();



if(grp.next()){



gs.log('Is member of group')


}



else{


                gs.log('Is not a member of group')


}



}