- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2023 07:50 AM
Hi Team,
I need to create catalog item when role and group selected, role has to be added to selected group.
Can we please help me out, Can i use flow designers to achieve easily or workflow.
Thank You
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2023 09:42 PM
Hi @Preethi26
try this:
var roles = [];
var role = new GlideRecord('sys_user_role');
role.addEncodedQuery('nameINitil,admin');
role.query();
while(role.next()){
roles.push(role.getUniqueValue());
}
gs.info(roles);
//var roleID = roles.split(',');/// comment this line, not needed
for (var i = 0; i < roles.length; i++) {
var grRole = new GlideRecord('sys_group_has_role');
grRole.initialize();
grRole.group = '019ad92ec7230010393d265c95c260dd';
grRole.role = roles[i];
grRole.setWorkflow(false);
grRole.insert();
}
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2023 09:55 PM
Hi @Preethi26
Can you try below script background script :
var roles = [];
var role = new GlideRecord('sys_user_role');
role.addEncodedQuery('nameINitil,admin');
role.query();
while(role.next()){
roles.push(role.getUniqueValue());
}
for (var i = 0; i < roles.length; i++) {
var grRole = new GlideRecord('sys_group_has_role');
grRole.initialize();
grRole.setValue('group','477a05d153013010b846ddeeff7b1225'); ///use sys_id of your group
grRole.setValue('role',roles[i]);
grRole.insert();
}
Also, check by putting some logs if you are getting sys_id of roles or name in workflow for 'current.variables.roles_to_be_added' variable. If you are getting the sys_ids then no need to loop through
var roles = current.variables.roles_to_be_added.toString().split(',');
for (var i = 0; i < roles.length; i++) {
var grRole = new GlideRecord('sys_group_has_role');
grRole.initialize();
grRole.setValue('group', current.variables.group); ///use sys_id of your group
grRole.setValue('role',roles[i]);
grRole.insert();
}
You can do this using flow designer also which is better option i guess...
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-01-2023 12:44 AM
Hi @Preethi26
Ok...so first you can add some condition in Look up Record - so that it will check records group & roles.
Step 1 :
Step 2:
Step 3 :
Final flow should look like below :
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2023 09:42 PM
Hi @Preethi26
try this:
var roles = [];
var role = new GlideRecord('sys_user_role');
role.addEncodedQuery('nameINitil,admin');
role.query();
while(role.next()){
roles.push(role.getUniqueValue());
}
gs.info(roles);
//var roleID = roles.split(',');/// comment this line, not needed
for (var i = 0; i < roles.length; i++) {
var grRole = new GlideRecord('sys_group_has_role');
grRole.initialize();
grRole.group = '019ad92ec7230010393d265c95c260dd';
grRole.role = roles[i];
grRole.setWorkflow(false);
grRole.insert();
}
Aman Kumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2023 10:03 PM
Did you overlook the response, I think I have already posted the correct response, and you missed it perhaps
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2023 10:06 PM
Thanks Aman, it worked in OOB instance by commenting the split line.
But if same logic applied for catalog item its not working. If for loop is removed its working.
Thanks for your support in my learning phase.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2023 09:55 PM
Hi @Preethi26
Can you try below script background script :
var roles = [];
var role = new GlideRecord('sys_user_role');
role.addEncodedQuery('nameINitil,admin');
role.query();
while(role.next()){
roles.push(role.getUniqueValue());
}
for (var i = 0; i < roles.length; i++) {
var grRole = new GlideRecord('sys_group_has_role');
grRole.initialize();
grRole.setValue('group','477a05d153013010b846ddeeff7b1225'); ///use sys_id of your group
grRole.setValue('role',roles[i]);
grRole.insert();
}
Also, check by putting some logs if you are getting sys_id of roles or name in workflow for 'current.variables.roles_to_be_added' variable. If you are getting the sys_ids then no need to loop through
var roles = current.variables.roles_to_be_added.toString().split(',');
for (var i = 0; i < roles.length; i++) {
var grRole = new GlideRecord('sys_group_has_role');
grRole.initialize();
grRole.setValue('group', current.variables.group); ///use sys_id of your group
grRole.setValue('role',roles[i]);
grRole.insert();
}
You can do this using flow designer also which is better option i guess...
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2023 10:04 PM
Thanks @Vishal Birajdar It worked for catalog item.
Flow designer i gave a try but list collector variable (roles to be added)is not accepted