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

Mark Manders
Mega Patron

In the Flow designer, everything is available to just configure this. Why use the workflow?


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

 

Our workflow is indeed complex, encompassing various conditions, script executions, and task management. While Flow Designer offers convenience, our requirements demand the robustness and flexibility provided by workflows. Incorporating these functionalities directly into the script enhances our ability to manage intricate business processes efficiently. However, we're also exploring whether certain aspects of this functionality could be separately implemented in Flow Designer and attached to the same catalog item. If you have any insights or suggestions on how we could leverage Flow Designer alongside workflows to streamline our process further, we're open to exploring those options."

Anubhav24
Mega Sage
Mega Sage

Hi @scn698 ,

After creating the tasks through script you will need to also add the variables to tasks as well for that you need to query the below tables to "item_option_new" using your catalog item name and then fetch the variables and then insert the variables and their values to this table "sc_item_variables_task".

 

In your script of creating a task you can first try to asses if the "access_prod" value is there or not using indexOf if yes then decrease the length of array simply by 1 if not then run the loop for the length of the array. This will help to decrease the if check for each value of the array.

 

To check if all tasks are closed or not you can simply write a if statement query the task table using RITM sysid and then check if all tasks's active field is true or false , if all tasks are not closed then you put a wait for condition in your workflow , based on that you can write your logic.

 

Please mark helpful/correct if my response helped you.

scn698
Tera Contributor

Thank you for your response! I appreciate the guidance provided. To implement the functionality described, I would need some code samples or examples to better understand the process. Specifically, I would like to

Having code examples would greatly assist me in implementing these functionalities in my script. Thank you in advance for your help!