Create Scheduled Script to add and remove users to group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2023 07:18 PM - edited 01-10-2023 07:19 PM
Hello,
I'm trying to use a scheduled script to add and remove users to groups from a catalog task when a specific assignment group has been assigned to the catalog task (Add or Remove End User to or from Group B, when Assignment Group C is assigned the catalog task.)
This is what I have now:
var gr = new GlideRecord('sys_user_grmember');
var gr_sc = new GlideRecord('sc_task');
gr_sc.addQuery('assignment_group', " GROUPC ");
gr_sc.addQuery('request_item.cat_item',"CAT_ITEM_SYS_ID");
gr_sc.query();
//Only Add or Remove users from group if "OPTIONA" is chosen on form
while (gr_sc.next()) {
var sa = gr_sc.request_item.variables.app;
var end_user = gr_sc.request_item.variables.end_user;
var at = gr_sc.request_item.variables.OPTIONB;
var requested_group = gr_sc.request_item.variables.GROUPB;
if ((sa == "OPTIONA") && ((requested_group != "GROUPB") || (requested_group != "GROUPB") || (requested_group != "GROUPB")) {
//Add users to group if above statement is met.
if (at == "Add Access") {
gr.addQuery('group', requested_group);
gr.initialize();
gr.user = end_user;
gs.info("End User & sa: " + end_user + " " + sa);
gr.insert();
}
//Remove users from group if above statement is met.
else if (at == "Remove Access") {// Remove access should be searching for already existing records in sys_user_grmember and deleting them
gr.addQuery('group', GROUPB);
gr.user = end_user;
gs.info("End User & sa: " + end_user + " " + sa);
gr.deleteRecord();
}
gs.info("sa= " + sa + " end_user = " + end_user + " at = " + at + " Requested Group = " + requested_group + " Assignment Group = " + gr_sc.request_item.variables.assignment_group); //Used for Debugging, also having issues pulling the catalog task's current assignment group.
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2023 07:41 PM - edited 01-10-2023 07:42 PM
I have read the code,and got some doubts。
1. why not use BR? i don't think scheduled script is a good idea.
2.
if ((sa == "OPTIONA") && ((requested_group != "GROUPB") || (requested_group != "GROUPB") || (requested_group != "GROUPB")) {
//Add users to group if above statement is met.
if (at == "Add Access") {
gr.addQuery('group', requested_group); <------ Here you want to insert a record, query condition will not work,
gr.initialize();
gr.user = end_user; <------Only user infor is setted, the group info is missing, i think you need to set the group info also.
gs.info("End User & sa: " + end_user + " " + sa);
gr.insert(); <---- befor inserting ,i think you need to check if the user is already setted to the group .
}
3.
//Remove users from group if above statement is met.
else if (at == "Remove Access") {// Remove access should be searching for already existing records in sys_user_grmember and deleting them
gr.addQuery('group', GROUPB);
gr.user = end_user; <---- here looks like setting a value to user felid ?! why not gr.addQuery() ?
gs.info("End User & sa: " + end_user + " " + sa);
gr.deleteRecord(); <---- gr.query() is missing.... add it and run delete command within gr.next()
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2023 07:47 PM - edited 01-10-2023 07:55 PM
Hi, your post does not make your issue clear.
Which parts of your code do work as expected?
Which parts of your code do not work as expected?
What are the results of your debugging?
I see 2 obvious issues
for insert you need to map both user and group
for delete you need to filter for a specific user, or delete all users for a group with deleteMultiple() or deleteRecord() in a while loop
your delete code is also missing query() method.
var gr = new GlideRecord('sys_user_grmember');
if (at == "Add Access") {
gr.initialize();
gr.user = end_user;
gr.group = requested_group;
gs.info("End User & sa: " + end_user + " " + sa);
gr.insert();
}
// your post doesn't make your requirements clear are you deleting 1 specific user from the group? If yes then
else if (at == "Remove Access") {
gr.addQuery('group', GROUPB);
gr.addQuery('user', end_user);
gr.query();
if(gr.next()) {
gs.info("End User & sa: " + end_user + " " + sa);
gr.deleteRecord();
}
}
//if deleting multiple users then
else if (at == "Remove Access") {
gr.addQuery('group', GROUPB);
gr.query();
gs.info("End User & sa: " + end_user + " " + sa);
gr.deleteMultiple();
}
//or
else if (at == "Remove Access") {
gr.addQuery('group', GROUPB);
gr.query();
while(gr.next()) {
gs.info("End User & sa: " + end_user + " " + sa);
gr.deleteRecord();
}
}
Note: edited the single user query to include an if condition check of result before delete.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2023 05:39 AM
Yes, I want to add or remove one specific user from group. The gs.info variables are all returning a value EXCEPT the assignment group, which is returning "undefined". How can I return that value via GlideRecord()?