How to make catalog item visible to the particular group and also users who do not have managers

Yakshitha
Tera Contributor

I have requirement to make the catalog item visible only based on below conditions
1. User should be member of "XYZ" group AND

2. User should not have manager assigned.

How to do this? Anyone can help me on this

 

 

 

@Ankur Bawiskar 

1 ACCEPTED SOLUTION

@Yakshitha 

if the logged in user is not member of that group and the next line tries to get the manager then it will throw error

try this logic

var inGroup = false;
var grMem = new GlideRecord('sys_user_grmember');
grMem.addQuery('user', user_id);
grMem.addQuery('group.name', 'XYZ'); // Exact match; use sys_id for precision
grMem.query();
if (grMem.hasNext()) {
    inGroup = true;
}

var hasManager = false;
var userRec = new GlideRecord('sys_user');
if (userRec.get(user_id)) { // Use built-in user_id for user criteria
    if (userRec.manager.toString().nil() == false) { // Safe manager check
        hasManager = true;
    }
}

answer = inGroup && !hasManager;

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

10 REPLIES 10

@Ankur Bawiskar 
 
Getting this error "Cannot read property "manager" from undefined"

@Yakshitha 

if the logged in user is not member of that group and the next line tries to get the manager then it will throw error

try this logic

var inGroup = false;
var grMem = new GlideRecord('sys_user_grmember');
grMem.addQuery('user', user_id);
grMem.addQuery('group.name', 'XYZ'); // Exact match; use sys_id for precision
grMem.query();
if (grMem.hasNext()) {
    inGroup = true;
}

var hasManager = false;
var userRec = new GlideRecord('sys_user');
if (userRec.get(user_id)) { // Use built-in user_id for user criteria
    if (userRec.manager.toString().nil() == false) { // Safe manager check
        hasManager = true;
    }
}

answer = inGroup && !hasManager;

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@Ankur Bawiskar 
This script is restricting all the members of "XYZ" group to access catalog item. Its not checking whether the user has manager or not.

@Yakshitha 

it should work fine

the 2nd part checks if logged in user has manager or not

If not and user is in member of that group then it allows

did you add gs.info() and debug?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@Ankur Bawiskar "

"Cannot find function nil in object" getting this error

Updated 

if (userRec.manager.toString().nil() == false) 


to 

if (userRec.manager.toString()!="") 

Its working now. Thank you @Ankur Bawiskar