Catalog item to add role to group

Preethi26
Tera Contributor

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

3 ACCEPTED SOLUTIONS

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();
}

Best Regards
Aman Kumar

View solution in original post

Vishal Birajdar
Giga Sage

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...

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

Hi @Preethi26 

 

Ok...so first you can add some condition in Look up Record - so that it will check records group & roles.

Step 1 :

VishalBirajdar_0-1701420061129.png

 

Step 2: 

VishalBirajdar_1-1701420130205.png

 

Step 3 : 

VishalBirajdar_2-1701420186621.png

 

Final flow should look like below :

 

VishalBirajdar_3-1701420254943.png

 

 

 

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

16 REPLIES 16

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Preethi26 

 

These are helpful link. Give a try and share teh feedback.

https://www.servicenow.com/community/itsm-forum/create-a-group-from-catalog-item-and-assign-couple-o...

https://www.servicenow.com/community/developer-forum/how-to-dynamically-add-role-to-group/m-p/151076...

https://www.servicenow.com/community/itsm-forum/automatically-add-role-to-group-via-script/m-p/74932...

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

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/atul_grover_lng [ Connect for 1-1 Session]

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

Hi @Dr Atul G- LNG 

Thanks,

I have created catalog item with group as reference , role as list field and created workflow but this script is not adding roles to group can you help.

var roles = [];
var role = new GlideRecord('sys_user_role');
role.addEncodedQuery('name','IN',current.variables.roles_to_be_added);
role.query();

while(role.next()){
roles.push(role.sys_id.toString());
}

var roleID = roles.split(',');
for (var i = 0; i < roleID.length; i++) {
var grRole = new GlideRecord('sys_group_has_role');
grRole.initialize();
grRole.group = current.variables.group;
grRole.role = roleID[i];
grRole.setWorkflow(false);
grRole.insert();
}

Preethi26_1-1701148060180.png

 

Aman Kumar S
Kilo Patron

Hi @Preethi26 

Update your script as below:

var roles = [];
var role = new GlideRecord('sys_user_role');
role.addEncodedQuery('nameIN' +current.variables.roles_to_be_added.toString());
role.query();
while(role.next()){
roles.push(role.getUniqueValue());
}

var roleID = roles.split(',');
for (var i = 0; i < roleID.length; i++) {
var grRole = new GlideRecord('sys_group_has_role');
grRole.initialize();
grRole.group = current.variables.group;
grRole.role = roleID[i];
grRole.setWorkflow(false);
grRole.insert();
}
Best Regards
Aman Kumar

Hi Aman,

I tried it but still its not added.

I have tried checking in background script as well in OOB instance but no success.

var roles = [];
var role = new GlideRecord('sys_user_role');
role.addEncodedQuery('nameINitil,admin');
role.query();
while(role.next()){
roles.push(role.getUniqueValue());
}

var roleID = roles.split(',');
for (var i = 0; i < roleID.length; i++) {
var grRole = new GlideRecord('sys_group_has_role');
grRole.initialize();
grRole.group = '019ad92ec7230010393d265c95c260dd';
grRole.role = roleID[i];
grRole.setWorkflow(false);
grRole.insert();
}