Updating variables on a task

rmcgowan
Giga Contributor

We have a catalog item that we need to loop over so that every time a catalog task is created, the variables on the task are updated.  On the catalog item form, one or more users can be selected from a slush bucket variable.  In the workflow there is a loop so that for every user that was selected, a catalog task is created and the short description of each one is updated with the name and user ID of each of the selected users.  The catalog task also has variables on it (that are hidden on the RITM and catalog item) that are supposed to populate with the rest of the users info (phone number, email, etc.)  The problem is that we haven't been able to set those variables correctly with the loop.  Here is what we have right now in the run script step of the workflow.  The bold part is where we are having trouble, trying to set the task variables.  This loop only works when there is only one user task being created.  When there is more than one user selected (and more than one task needs to be created), the variables on both tasks are being set with the info of only one of the users.  As in, User 1's task will have User 2's info in the variables instead of User 1's, for example.  We are assuming at this is happening because the variable pool is part of the RITM instead of the task-level.  We can set them correctly with a client script, but we want to have the variables permanently set for reporting purposes and not just on-load with the client script because then the fields on the report are empty.  Please let me know if any other details are needed.  Thank you.

var allTrainees = [];

allTrainees = current.variables.training_class_members.getValue();

var sepTrainee = [];

var sepTrainee = allTrainees.split(',');

var length = sepTrainee.length;

for(var i=0; i<length;i++){
var user = new GlideRecord('sys_user');
user.addQuery('sys_id', sepTrainee[i].trim());
user.query();
user.next();
var task = new GlideRecord("sc_task");
task.initialize();
task.parent = current.sys_id;
task.short_description = "Training activation request for" + " " + user.getValue('name') + " - " + user.getValue('user_name');
task.request=current.request;
task.request_item = current.sys_id;
task.due_date = current.due_date;
task.assignment_group = 'dd966faf138c260053335f722244b0fc';
current.variable_pool.trainee_name = user.sys_id;
current.variable_pool.trainee_unid = user.user_name;
current.variable_pool.trainee_email = user.email;
current.variable_pool.trainee_phone = user.phone;
current.variable_pool.trainee_department = user.department;

task.insert();
}

1 ACCEPTED SOLUTION

ccajohnson
Kilo Sage

Keep in mind that Variables within a Catalog Item record are shared with all of the tasks associated with that Item. Because you are re-using those variables, you will overwrite them and not have the variables used in the manner you intend to use them for.

What I would suggest instead is to create multiple Requested Items (one for each person in the slush bucket) that way you can fulfill each separately and still connect them all to the parent Requested Item.

View solution in original post

2 REPLIES 2

ccajohnson
Kilo Sage

Keep in mind that Variables within a Catalog Item record are shared with all of the tasks associated with that Item. Because you are re-using those variables, you will overwrite them and not have the variables used in the manner you intend to use them for.

What I would suggest instead is to create multiple Requested Items (one for each person in the slush bucket) that way you can fulfill each separately and still connect them all to the parent Requested Item.

Thank you, that's a great suggestion.  I will look into this.