If a user has a role dont create a task
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-29-2024 12:20 PM
Hello, recently ive had to create tasks based on how many roles are in a group that a user requests asking to, below is code for creating that. Now, ive come across another scenario, now i need to still create the tasks however if they already have a role that the group they are requesting access to then there is no need to create the task. Basically, user has a role provided by a group than dont generate the task, anyone know how i could do that, i know i have to create a New GlideRecord with the sys_user_has_role but im having difficulty, if anyone has time to answer or any tips.
Below is code for making tasks based on how many roles are in a group.
var grGroupRoles = new GlideRecord('sys_group_has_role');
grGroupRoles.addQuery('group', current.variables.role);
grGroupRoles.query();
while (grGroupRoles.next()) {
var task =new GlideRecord("sc_task"); //code below is creating tasks based on how many roles are in a group
task.initialize();
task.setValue('request_item', current.getValue('sys_id'));
task.short_description ='Account Creator Review & Sign';
task.requested_for ='requested_for';
task.description = current.variables.role.getDisplayValue();
task.approval ='requested';
task.assignment_group = '9c65f81c1b366c1091c5a820f54bcb2a';
task.insert();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-29-2024 12:48 PM
Hey @HenryD,
What is the need to stop adding requestors to a group if they already have a role associated with that group?
Because that group may be used in other processes such as ticket assignment, notifications, etc.
Cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-29-2024 12:52 PM
Hello, the role associated to a group is from the first group, we have something in place like a "modification" where they can select to be in another group but they may have the same role in that group, thus not needing to create another task if they already have that role
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-29-2024 01:49 PM - edited ‎02-29-2024 01:49 PM
Hey @HenryD,
Thanks for that, but I assume you would still need a task to remove the user from the 'first group' and add the user to the new group.
Anyway, the following script may help with your requirement:
var firstGroupRoles = new GlideRecord('sys_group_has_role');
firstGroupRoles.addQuery('group', //specify the first group here);
firstGroupRoles.query();
var firstGroupArr = []; //contains all the roles associated with the first group
while(firstGroupRoles.next())
{
firstGroupArr .push(firstGroupRoles.getValue('role'));
}
var newGroupRoles = new GlideRecord('sys_group_has_role');
newGroupRoles.addEncodedQuery('group', //specify the second group here);
var newGroupArr = []; //contains all the roles associated with the first group
while(newGroupRoles.next())
{
newGroupArr .push(newGroupRoles.getValue('role'));
}
//Compare if two arrays are identical
var arrayUtil = new ArrayUtil();
if(arrayUtil.diff(firstGroupArr , newGroupArr ).length == 0)
{
//No difference in roles between 2 groups
}
else
{
//There are differences, create task here
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-29-2024 08:40 PM
Hello, yes once all these tasks get approved there is a script at the end of the workflow that removes the user from the old group into the new group, aplogies for the confusion