- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2022 11:04 PM - edited 11-01-2022 11:11 PM
Hi guys,
I have a list collector in the group table that has a list of roles in it.
I need to check if those roles are already contained in that group (sys_group_has_role table). And if not, assign them.
I came up with a business rule in sys_user_group
var list = current.list_collector_variable.toString();
var listSplit = list.split(',');
var gr = new GlideRecord("sys_group_has_role");
gr.query("group", "current.sys_id");
gr.query();
while (gr.next()) {
for (var i = 0; i < listSplit.length; i++) {
if (gr.role == listSplit[i]) {
gs.log("duplicated");
} else {
gr.initialize();
gr.group = current.sys_id;
gr.role = arr[i];
gr.insert();
gs.log("created");
}
}
}
I have done it in multiple ways, I guess I'm thinking about it in the wrong way, because it is not working as expected.
Am I in the right way? Any tip is appreciated!
Solved! Go to Solution.
- Labels:
-
Service Level Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2022 11:28 PM
Hi @JoaoV ,
Try below script:
var group = current.getUniqueValue();
var list = current.getValue('list_collector_variable').split(',');
list.forEach(function(item){
var gr = new GlideRecord("sys_group_has_role");
gr.addQuery("group", group);
gr.addQuery("role", item)
gr.query();
if(!gr.next()){
var grM = new GlideRecord('sys_group_has_role')
grM.initialize();
grM.role = item;
grM.group = group;
grM.insert()
}
});
I hope this help.
Please mark this helpful if this helps and Accept the solution if this solves your issue.
Regards,
Kamlesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2022 11:25 PM - edited 11-01-2022 11:55 PM
what issue are you getting?
two issue which I can figure out are :
1. for (var i = 0; i <= listSplit.length; i++) // <= instead of < as loop starts from 0
2. gr.role =listSplit[i]; // it should not be arr[i]
Please mark the answer correct/helpful accordingly.
Raghav
MVP 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-01-2022 11:28 PM
Hi @JoaoV ,
Try below script:
var group = current.getUniqueValue();
var list = current.getValue('list_collector_variable').split(',');
list.forEach(function(item){
var gr = new GlideRecord("sys_group_has_role");
gr.addQuery("group", group);
gr.addQuery("role", item)
gr.query();
if(!gr.next()){
var grM = new GlideRecord('sys_group_has_role')
grM.initialize();
grM.role = item;
grM.group = group;
grM.insert()
}
});
I hope this help.
Please mark this helpful if this helps and Accept the solution if this solves your issue.
Regards,
Kamlesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-02-2022 12:08 AM
It works great @kamlesh kjmar ! I cannot thank you enough... Been hammering on this all the day!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-02-2022 12:25 AM
I am glad, it helped you 🙂