Assign catalog task to the multiple assignment groups selected on catalog item variable in workflow

Community Alums
Not applicable

Hello Experts,

 

I have list collector variable (assignment_group) on catalog item form referencing to a custom table and all those groups are present in sys_user_group table, I want to trigger catalog tasks to any group selected on catalog item variable while raising a request. If one group is selected then one catalog task should tigger to that particular assignment group. If multiple groups are selected that multiple catalog task should trigger to those assignment groups.

 

Please advice how can I achieve this.

Script in the catalog task activity:

var assgnmntGroup = current.variables.assignment_group.getDisplayValue();
var groupArray = assgnmntGroup.split(',');
var groupName = [];
var groupnames = [];
for (i = 0; i < groupArray.length; i++) {
    groupName = groupArray[i].trim();
    var gr = new GlideRecord('u_common_choice_values');
    gr.addEncodedQuery('u_active=true^u_catalog_item=b511385ec32a52909275be53e4013121^u_variable=de83da3d97f256d002e4b2e8c253afe7^u_valueIN' + current.variables.assignment_group.getDisplayValue());
    gr.addQuery('u_value', groupName.trim());
    gr.query();
    if (gr.next()) {
        var groups = new GlideRecord('sys_user_group');
        groups.addEncodedQuery('nameIN' + current.variables.assignment_group.getDisplayValue());
        groups.addQuery('name', groupName.trim());
        groups.query();
        if (groups.next()) {
            groupnames.push(groups.sys_id.toString());
            for (j = 0; j < groupnames.length; j++) {
                var groupSysIds = groupnames[j];
                gs.info('logs for groups ' + groupSysIds);
                task.short_description = "clusters";
                task.description = "This is a request to clusters";
                task.assignment_group = groupSysIds;
            }
        }
    }
}


With the above script am able to create only one catalog task getting assigned to last assignment group in the array. remaining are groups are ignored.


Please help me to achieve this
1 ACCEPTED SOLUTION

Community Alums
Not applicable

Yes the tasks needed to be created parallelly. 
i used  the run script activity to achieve this scenario and it worked.
Here is the modified script:

var assgnmntGroup = current.variables.assignment_group;
if (assgnmntGroup) {
    var assgnmntgroupnames = assgnmntGroup.getDisplayValue();
    if (assgnmntgroupnames) {
        var groupArray = assgnmntgroupnames.split(',');
        for (i = 0; i < groupArray.length; i++) {
            var groupName = groupArray[i].trim();
            if (groupName) {
                var groups = new GlideRecord('sys_user_group');
                groups.addQuery('name', groupName);
                groups.query();
                if (groups.next()) {
                   // gs.info('hare hare one  ' + groupName);
                    var task = new GlideRecord('sc_task');
                    task.initialize();
                    task.short_description = "clusters";
                    task.description = "This is a request to clusters" ;
                    task.assignment_group = groups.sys_id;
                    task.request_item = current.sys_id;

                    task.insert();

         
                }
            }
        }
    }
}



View solution in original post

6 REPLIES 6

Uncle Rob
Kilo Patron

VERY important detail here:   Do you expect all these Tasks to be "in parallel", and must all of them finish in order to move to the next part of the workflow?

Also, scripting this isn't a good idea because the script created sc_tasks aren't REALLY part of the flow at that point.

Community Alums
Not applicable

Yes the tasks needed to be created parallelly. 
i used  the run script activity to achieve this scenario and it worked.
Here is the modified script:

var assgnmntGroup = current.variables.assignment_group;
if (assgnmntGroup) {
    var assgnmntgroupnames = assgnmntGroup.getDisplayValue();
    if (assgnmntgroupnames) {
        var groupArray = assgnmntgroupnames.split(',');
        for (i = 0; i < groupArray.length; i++) {
            var groupName = groupArray[i].trim();
            if (groupName) {
                var groups = new GlideRecord('sys_user_group');
                groups.addQuery('name', groupName);
                groups.query();
                if (groups.next()) {
                   // gs.info('hare hare one  ' + groupName);
                    var task = new GlideRecord('sc_task');
                    task.initialize();
                    task.short_description = "clusters";
                    task.description = "This is a request to clusters" ;
                    task.assignment_group = groups.sys_id;
                    task.request_item = current.sys_id;

                    task.insert();

         
                }
            }
        }
    }
}



Ankur Bawiskar
Tera Patron
Tera Patron

@Community Alums 

why are you creating catalog task via script?

you might be using either Workflow or flow designer.

Why not add logic in that itself?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Community Alums
Not applicable

i have added the logic in catalog task activity itself but it was throwing a warning of data policy exception.
Later removed the catalog task activity and used run script activity it worked.
thanks for the quick response.