User Criteria

Milind1
Tera Contributor

My requirement is to restrict visibility of catalog based on the group. We want dynamic script so that only *SDM groups can see that items. any lead will be helpful 

1 ACCEPTED SOLUTION

Vishal Birajdar
Giga Sage

Hello Milind,

You can try this code in script part of User criteria :

 

/** Scripted User Criteria is not cached, and evaluated everytime, so performance is dependent on the script.
* Populate `answer` with true/false or evaluate to true/false
* The script is evaluated in the scope the user criteria is defined
* Don't use `current` in the script or populate the variable
* Don't use `gs.getUser()` or `gs.getUserID()`,
* instead use `user_id` which contains the user sys_id against whom the evaluation is happening.
*/

answer = isUserMember(user_id);
function isUserMember(user_id) {
var isMember = false;
var gpMember = new GlideRecord("sys_user_grmember");
gpMember.addEncodedQuery('group.nameLIKESDM^user=' + user_id);
gpMember.query();
if (gpMember.next()) {
isMember = true;
}

return isMember;
}

 

VishalBirajdar7_0-1677163173234.png

 

 

You can check this using 'User Criteria Diagnostics'

 

VishalBirajdar7_1-1677163297717.png

 

 

Vishal Birajdar
ServiceNow Developer

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

View solution in original post

5 REPLIES 5

shivangi k
Kilo Sage

Hi @Milind1 ,

 

You can use related list "Available for" in catalog and create a record

shivangik_0-1677157022041.pngshivangik_1-1677157095772.png

You can add group name and catalog will only be visible to SDM group.

 

Please mark helpful and accept solution if it helped you

 

Regards,

Milind1
Tera Contributor

As we have 100+ groups that contains SDM keyword.  we want dynamic solution using script. below I have written a script but it is not working correctly

var grpsel = [];
var rec = new GlideRecord('sys_user_grmember');
rec.addQuery('user', gs.getUserID());
rec.query();
while (rec.next()) {
grpsel.push(String(rec.group));
}
var grp = new GlideRecord('sys_user_group');
grp.addEncodedQuery("nameLIKEsdm^sys_idIN" + grpsel);
grp.query();
if (grp.hasNext()) {
answer = true;
} else {
answer = false;
}

Hi @Milind1 ,

 

Try below code:

 

var grpsel = [];
var rec = new GlideRecord('sys_user_grmember');
rec.addQuery('user', gs.getUserID());
rec.query();
while (rec.next()) {
grpsel.push(rec.group);
}
var grp = new GlideRecord('sys_user_group');
grp.addQuery("sys_id" , grpsel);
grp.addEncodedQuery("nameLIKEsdm");
grp.query();
while (grp.hasNext()) {
answer=true;
}

 

Please mark helpful and accept solution if it helped you.

 

Regards,

Vishal Birajdar
Giga Sage

Hello Milind,

You can try this code in script part of User criteria :

 

/** Scripted User Criteria is not cached, and evaluated everytime, so performance is dependent on the script.
* Populate `answer` with true/false or evaluate to true/false
* The script is evaluated in the scope the user criteria is defined
* Don't use `current` in the script or populate the variable
* Don't use `gs.getUser()` or `gs.getUserID()`,
* instead use `user_id` which contains the user sys_id against whom the evaluation is happening.
*/

answer = isUserMember(user_id);
function isUserMember(user_id) {
var isMember = false;
var gpMember = new GlideRecord("sys_user_grmember");
gpMember.addEncodedQuery('group.nameLIKESDM^user=' + user_id);
gpMember.query();
if (gpMember.next()) {
isMember = true;
}

return isMember;
}

 

VishalBirajdar7_0-1677163173234.png

 

 

You can check this using 'User Criteria Diagnostics'

 

VishalBirajdar7_1-1677163297717.png

 

 

Vishal Birajdar
ServiceNow Developer

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