- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2024 05:52 PM
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();
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-29-2024 10:50 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-29-2024 07:07 AM
Hi @scn698 ,
For what steps you need sample scripts , can you please specify I will try to provide them here.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-29-2024 07:55 AM - edited ‎02-29-2024 09:39 AM
In my above script when user selects the product, the specific code is not working and also I have the same script for another activity it's working fine and also still it's creating task for prod as well
Example, if user selects prod and dev it should execute the script specified in the if condition and also it should create a catalog task only for dev but as of now it creating the tasks for both prod and dev and in short description of catalog task it's displaying the sysid instead of the display values, it's urgent requirement I need to accomplish as soon as possible,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-29-2024 10:31 AM - edited ‎02-29-2024 10:34 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-29-2024 10:40 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-29-2024 10:50 AM
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.