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

Dr Atul G- LNG
Tera Patron

@Yakshitha 

You need to create the script this and then identify the user based on the criteria.

 

something:

 

function checkUserConditions(userSysId) { var userSysId = userSysId || gs.getUserID(); // Default to current user // Condition 1: Check if user is a member of "XYZ" group var groupMember = new GlideRecord('sys_user_grmember'); groupMember.addQuery('user', userSysId); groupMember.addQuery('group.name', 'XYZ'); groupMember.query(); var isInGroup = groupMember.hasNext(); // Condition 2: Check if user has no manager assigned var user = new GlideRecord('sys_user'); user.get(userSysId); var hasNoManager = user.isValidRecord() && user.getValue('manager') == null || user.getValue('manager') == ''; return isInGroup && hasNoManager; } // Example usage - can be called from Business Rule, Script Include, etc. var result = checkUserConditions(gs.getUserID()); gs.info('User meets conditions: ' + result);

*************************************************************************************************************
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/dratulgrover [ Connect for 1-1 Session]

****************************************************************************************************************

Dr Atul G- LNG
Tera Patron

(function (/*GlideRecord*/ user) {

// Condition 1: Check if user is a member of "XYZ" group
var groupMember = new GlideRecord('sys_user_grmember');
groupMember.addQuery('user', user.sys_id);
groupMember.addQuery('group.name', 'XYZ'); // Replace with your exact group name
groupMember.setLimit(1);
groupMember.query();

var isInXYZGroup = groupMember.hasNext();

// Condition 2: Check if user has NO manager assigned
var hasNoManager = user.getValue('manager') === null || user.getValue('manager') === '';

// Both conditions must be true for the item to be visible
return isInXYZGroup && hasNoManager;

})(user);

*************************************************************************************************************
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/dratulgrover [ Connect for 1-1 Session]

****************************************************************************************************************

Ankur Bawiskar
Tera Patron

@Yakshitha 

you can use User Criteria with script

Then add this in "Available For" related list on that item

1) visit that catalog item and click New in Related list for "Available For"

56.png

2) then create this below user criteria

Note: it's recommended to use user_id in user criteria script and not gs.getUser() etc. check below KB

user_id variable usage for user criteria 

var user = user_id;

var memberRec = new GlideRecord('sys_user_grmember');
memberRec.addQuery('user', user);
memberRec.addQuery('group.name', 'XYZ'); // Exact group name
memberRec.query();
var inGroup = memberRec.hasNext();

// Check if user has no manager
var hasManager = !gs.nil(memberRec.user.manager.toString());

answer = inGroup && !hasManager;

55.png

💡 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

@Yakshitha 

Hope you are doing good.

Did my reply answer your question?

💡 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