If a user has a role dont create a task

HenryD
Tera Guru

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

}

7 REPLIES 7

Hello, ive used this code to match mine, its seems its still making the other roles based on number tasks inside the group, ive copied and pasted the code below, basically it is all of yours and what was on top and mine on the "Else" what is Bolded, i think this is because im still querying the check to see how many roles there are in the group, what would you suggest i do to replace that? 

 

 

 

var firstGroupRoles = new

GlideRecord('sys_group_has_role');

firstGroupRoles.addQuery('group', current.variables.current_groupname);

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', current.variables.groupname);

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

{

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

}

Hey @HenryD,

 

Apologies, had a few typos there. Try the following:

 

var firstGroupRoles = new GlideRecord('sys_group_has_role');
firstGroupRoles.addQuery('group', current.variables.current_groupname);
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.addQuery('group', current.variables.groupname);
newGroupRoles.query();
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
{

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

    }
}

Hello, im searching threw the code to see if i made a typo, but basically right now im comparing the two groups selected in the catalog item, and the groups have 3 roles in it & im testing that they share 1 role, right now its creating 3 tasks instead of 2 (since they share a role, 1 less task is needed since they already have that role.) im not sure what it could be that isnt working