Script Include Help !!

SandeepKSingh
Kilo Sage

You need to create a Script Include that contains a reusable function for validating whether a user is part of a particular group (e.g., "Network Support"). How would you write the Script Include?

3 ACCEPTED SOLUTIONS

Mark Manders
Mega Patron

Where is this question coming from? You are asking for help, which indicates that you have a script include with which you need help. And then you start your question with "You need to create....".

It is your question right? Why do I need to create anything?

 

What did you already try, where are you stuck? 

The community isn't a place for you to get others to do your work for you for free. Try it. Test it. And if it doesn't work, tell us what you did, what you expected and what the result was and we will help.

 

Or, in case of such a simple script include, use Google. There are lots of examples out there that you just have to tweak to your own requirements.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

View solution in original post

Ravi Gaurav
Giga Sage
Giga Sage

Hello @SandeepKSingh 

Here’s how you can write a reusable Script Include in ServiceNow to validate whether a user belongs to a specific group:
isUserInGroup: function (userId, groupName) {
if (!userId || !groupName) {
gs.error("User ID and Group Name are required parameters.");
return false;
}

var groupGR = new GlideRecord('sys_user_group');
groupGR.addQuery('name', groupName);
groupGR.query();

if (groupGR.next()) {
var groupId = groupGR.getValue('sys_id');
var userGR = new GlideRecord('sys_user_grmember');
userGR.addQuery('user', userId);
userGR.addQuery('group', groupId);
userGR.query();
return userGR.hasNext();
} else {
gs.error("Group not found: " + groupName);
return false;
}
},

 

 

 

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

View solution in original post

Runjay Patel
Giga Sage

Hi @SandeepKSingh ,

 

you can create script include and use below code. You can call this function whenever you need to check.

var GroupMembershipValidator = Class.create();
GroupMembershipValidator.prototype = {
initialize: function() {
},

// Function to check if a user is a member of a specific group
isUserInGroup: function(userId, groupName) {
// Create a GlideRecord object to query the Group table (sys_user_group)
var groupGr = new GlideRecord('sys_user_group');

// Check if the group exists based on the group name
if (!groupGr.get('name', groupName)) {
gs.error('Group "' + groupName + '" does not exist.');
return false;
}

// Now check if the user is a member of the group
var userGroupGr = new GlideRecord('sys_user_grmember');
userGroupGr.addQuery('user', userId);
userGroupGr.addQuery('group', groupGr.getUniqueValue());
userGroupGr.query();

// If a matching record is found, return true, meaning the user is in the group
if (userGroupGr.next()) {
return true;
}

// If no record is found, the user is not in the group
return false;
},

type: 'GroupMembershipValidator'
};

Accept the solution if it helped for you.

 

View solution in original post

3 REPLIES 3

Mark Manders
Mega Patron

Where is this question coming from? You are asking for help, which indicates that you have a script include with which you need help. And then you start your question with "You need to create....".

It is your question right? Why do I need to create anything?

 

What did you already try, where are you stuck? 

The community isn't a place for you to get others to do your work for you for free. Try it. Test it. And if it doesn't work, tell us what you did, what you expected and what the result was and we will help.

 

Or, in case of such a simple script include, use Google. There are lots of examples out there that you just have to tweak to your own requirements.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Ravi Gaurav
Giga Sage
Giga Sage

Hello @SandeepKSingh 

Here’s how you can write a reusable Script Include in ServiceNow to validate whether a user belongs to a specific group:
isUserInGroup: function (userId, groupName) {
if (!userId || !groupName) {
gs.error("User ID and Group Name are required parameters.");
return false;
}

var groupGR = new GlideRecord('sys_user_group');
groupGR.addQuery('name', groupName);
groupGR.query();

if (groupGR.next()) {
var groupId = groupGR.getValue('sys_id');
var userGR = new GlideRecord('sys_user_grmember');
userGR.addQuery('user', userId);
userGR.addQuery('group', groupId);
userGR.query();
return userGR.hasNext();
} else {
gs.error("Group not found: " + groupName);
return false;
}
},

 

 

 

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

Runjay Patel
Giga Sage

Hi @SandeepKSingh ,

 

you can create script include and use below code. You can call this function whenever you need to check.

var GroupMembershipValidator = Class.create();
GroupMembershipValidator.prototype = {
initialize: function() {
},

// Function to check if a user is a member of a specific group
isUserInGroup: function(userId, groupName) {
// Create a GlideRecord object to query the Group table (sys_user_group)
var groupGr = new GlideRecord('sys_user_group');

// Check if the group exists based on the group name
if (!groupGr.get('name', groupName)) {
gs.error('Group "' + groupName + '" does not exist.');
return false;
}

// Now check if the user is a member of the group
var userGroupGr = new GlideRecord('sys_user_grmember');
userGroupGr.addQuery('user', userId);
userGroupGr.addQuery('group', groupGr.getUniqueValue());
userGroupGr.query();

// If a matching record is found, return true, meaning the user is in the group
if (userGroupGr.next()) {
return true;
}

// If no record is found, the user is not in the group
return false;
},

type: 'GroupMembershipValidator'
};

Accept the solution if it helped for you.