Not applicable

Hi Arjun,


Can you try the following script. 

answer = ifScript();

function ifScript() {
varusr=current.variables.requested_for; //Requsted for user
vargr=newGlideRecord('sys_user_grmember');
gr.addEncodedQuery('user='+usr+'^group.name=Director Group');
gr.query();
if (gr.next()) {
return'yes';
}
return'no';
}
 
Please mark the answer helpful or correct based on the impact of the answer.
 
Thanks
Ishan Parikh

View solution in original post

Hi Ishan, 

 

Thanks for the help on this, 

As I'm new to ServiceNow it will be very helpful if you kindly help with this part of the query,

gr.addEncodedQuery('user='+usr+'^group.name=Director Group');

 

Regards

Arjun

Not applicable

Hi Arjun, 

What help do you need from that line of the code ? 

If you mean helping you understand - it does the following 

The addEncodedQuery basically builds up the filter that you would like to apply on the glide record (table). 

the two attributes that you have on the group membership table are 

1) user 2) group 

you want that the requested for user must be member of a particular group (Director group) and if it is true then return yes 

so in this we are building that query 

'user=' --> is the attribute on the membership table.

+usr+ --> dynamic value of the requested for user value.

'group.name=Director Group' --> search for the group name value (exact match with Director Group) value. 

Thanks
Ishan Parikh

 

Omkar Mone
Mega Sage

Hi 

Please try the below script :- 

 

var arr=[];
var usr=current.variables.requested_for;
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', usr);
gr.query();
while(gr.next())
{
arr.push(gr.sys_id.toString());
}
for(var j=0;j<arr.length;j++)
{
  checkForEveryGroup(arr[j]);
}


function checkForEveryGroup(eachGroup)
{
//here check your condition
//each group has now the groups in which the user is in. It will call this function for every iteration and do you checkinng.


}

Obah
Tera Contributor

Hi 

Business Rule *before*

This works perfect for preventing duplicate record entries to the Group Member table (sys_user_grmember)

Remember to add a filter if you are targeting ONLY a specific group.

(function executeRule(current, previous /*null when async*/) {
       // Add your code here

 var rec = new GlideRecord('sys_user_grmember');
rec.addEncodedQuery('group.name=your_groupname^active=true');//Action to apply only to members of this group
rec.addQuery('user',current.user);
rec.query();
if(rec.getRowCount() == 1){
gs.addErrorMessage(current.user.getDisplayValue() + " is already high risk asset owner");
current.setAbortAction(true);

}


})(current, previous);