Need help with creating multiple catalog tasks and waiting for all tasks to complete

scn698
Tera Contributor

I'm working on a requirement where I need assistance with creating multiple catalog tasks based on user selections, excluding one specific value (let's say "PROD"). Additionally, I need guidance on how to ensure the workflow waits until all the created tasks are complete before proceeding to the next activity.

Specifically, here's what I'm trying to achieve:

 

1. **Creating Tasks:** When users select values from a list collector field, I want to create separate catalog tasks for each selected value, except for "PROD".
2. **Executing Specific Script:** If "PROD" is selected along with other values, I need to execute a specific script without creating a catalog task for "PROD".
3. **Waiting for Task Completion:** Once the tasks are created, I want the workflow to pause until all the catalog tasks associated with the request item are closed.

I've attempted to implement this using a "Run Script" activity in my workflow, but I'm encountering issues with creating the correct number of tasks and ensuring the workflow waits for all tasks to complete.

Any advice or sample scripts on how to achieve this would be greatly appreciated!

---

var environment = [];
environment = current.variables.environment;
var choices = environment.toString().split(',');

// Check if "access_prod" is selected
var prodSelected = choices.indexOf("access_prod") !== -1;

// Execute script for "access_prod" if selected
if (prodSelected) {
   
    var from = new GlideRecord('sys_user_grmember');
    from.addQuery('group', current.variables.select_your_group);
    from.query();
    while (from.next()) {
        var into = new GlideRecord('sys_user_grmember');
        into.addQuery("user", current.variables.user_for);
        into.addQuery("group", from.group);
        into.query();
        if (!into.hasNext()) {
            into.initialize();
            into.user = current.variables.user_for;
            into.group = from.group;
            into.insert();

            // Fetch display name of the group
            var groupGr = new GlideRecord('sys_user_group');
            if (groupGr.get(from.group)) {
                var groupName = groupGr.getDisplayValue();
                var targetUser = new GlideRecord('sys_user');
                if (targetUser.get(current.variables.user_for)) {
                    var targetUserName = targetUser.getDisplayValue();
                    gs.info("Added group " + groupName + " to user " + targetUserName);
                }
            }
        }
    }
}

// Create separate catalog tasks for all selected environment values other than "access_prod"
for (var i = 0; i < choices.length; i++) {
    if (choices[i] !== "access_prod") {
        var task = new GlideRecord("sc_task");
        task.initialize();
        task.parent = current.sys_id;
        task.request = current.request;
        task.short_description = "Catalog task for " + choices[i]; // Set a short description indicating the environment value
        task.request_item = current.sys_id;
        task.assignment_group = choices[i];
        task.insert();
    }
}
1 ACCEPTED SOLUTION

If it is a list collector variable then it will always return a list of sys_ids and not the display value you are looking for , that explains why you are seeing the sys_ids in your short description field and the reason why it is creating tasks for both prod and dev because your if condition [ if (choices[i] !== "access_prod") ] is always true because in choices[i] is sysid and when compared to the value access_prod it is not equal and tasks are created , to overcome this , can you try 

"environment = current.variables.environment.getDisplayValue();"

If above does not works then using the sys_ids you need to query the table which is being referenced by this list collector variable and then get the display values of the environment.

 

Please mark helpful/correct if my response helped you.

View solution in original post

10 REPLIES 10

scn698
Tera Contributor

Thanks @Anubhav24 it is working as expected now.