Catalog Item Variable Validation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2025 12:33 AM - edited 06-19-2025 12:37 AM
Hi All,
There is a catalog item which is to create ServiceNow Groups automatically. I have written below run script in the workflow.
Everything is working fine and the group is getting created in ServiceNow, but the "type" values are not getting added. Please help.
In Group table 'type' is a 'list' type field and is reference to the "sys_user_group_type" table. And in the catalog item the variable is a list collector and choices are from question_choice table.
Run Script:
// Get the current catalog item's variables
var groupName = current.variables.group_servicenow;
var groupManager = current.variables.group_manager;
var groupMembers = current.variables.members_of_the_group; // List collector (comma-separated sys_ids)
var groupDesc = current.variables.description;
var groupIntentionIds = current.variables.nature_of_the_group;
// Determine group type names based on intention
var groupTypeNames = [];
if (groupIntentionIds) {
var intentionArray = groupIntentionIds.toString().split(',');
for (var i = 0; i < intentionArray.length; i++) {
var choiceGR = new GlideRecord('question_choice');
choiceGR.addQuery('sys_id', intentionArray[i].trim());
choiceGR.query();
if (choiceGR.next()) {
var displayValue = choiceGR.value.toString(); // or use choiceGR.text if needed
if (displayValue == 'Approval Group') {
groupTypeNames.push('Approvals Module');
}
if (displayValue == 'Taking Tickets') {
groupTypeNames.push('!All Module Access');
}
}
}
}
// Create the group
var group = new GlideRecord('sys_user_group');
group.initialize();
group.name = groupName;
group.manager = groupManager;
group.description = groupDesc;
var groupSysId = group.insert();
gs.info('Group created with Sys ID: ' + groupSysId);
// Link group to types using sys_user_group_has_type
for (var j = 0; j < groupTypeNames.length; j++) {
var typeGR = new GlideRecord('sys_user_group_type');
typeGR.addQuery('u_display_name', groupTypeNames[j]);
typeGR.query();
if (typeGR.next()) {
var groupTypeLink = new GlideRecord('sys_user_group_has_type');
groupTypeLink.initialize();
groupTypeLink.group = groupSysId;
groupTypeLink.type = typeGR.sys_id;
groupTypeLink.insert();
gs.info('Linked group to type: ' + groupTypeNames[j]);
} else {
gs.warn('Group type not found: ' + groupTypeNames[j]);
}
}
// Add members to the group
if (groupMembers) {
var membersArray = groupMembers.toString().split(',');
for (var i = 0; i < membersArray.length; i++) {
var memberId = membersArray[i].trim();
if (memberId) {
var groupMember = new GlideRecord('sys_user_grmember');
groupMember.initialize();
groupMember.user = memberId;
groupMember.group = groupSysId;
groupMember.insert();
gs.info('Added member to group: ' + memberId);
}
}
}
Please assist.
Thanks & Regards.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2025 01:00 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2025 01:02 AM
Hi @Joshuu
I suggest that instead of querying the Choice List table to get the sys ids using variable values, you store the sys_ids of the two group types in a system property and use them directly in your script. This will make your code easier to manage.
Regards,
Siva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2025 01:04 AM
Hi @J Siva ,
How can we use it, could you please explain me in detail.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-19-2025 01:12 AM
then query group type table with that label and get sysId and then store
you are already querying that table with u_display_name then it's not working?
what came in that groupTypeNames array?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader