How to include variables on SC_TASK form using script?

rajeshraya
Giga Expert

Hi Everyone,

I had a requirement to create number of catalog tasks in my workflow dynamically.

When I say dynamically, I mean number of tasks to be created was determined number of values in a list collector variable of the catalog item.

Obviously, one cannot leverage CATALOG TASK activity in their workflow to achieve this. But, SC_TASK table can be glided using a RUN SCRIPT activity in the workflow.

When the SC_TASK is glided for creating those tasks, catalog task variables cannot be displayed on such tasks as you don't have option to add the variables to be displayed on the form like you do with a CATALOG TASK activity.

Since I could not add those variables, client scripts expecting those variables on the form were causing the form to come stand still (users were not able to even update work notes on task. all they could do was CLOSE the TASK).

To address this, I came up with a piece of code which worked out well to bring those variables on to sc_task form. Below is the code which creates number of tasks dynamically and includes variables on task. Hope this helps in resolving similar issues for SNOW community.

****************************************************************************************************************************************************************************************************

createCatalogTasks();

function createCatalogTasks() {

  var list = current.variables.list_collector_var1.toString();

  var arrayList = list.split(",");

  for (var idx = 0; idx < arrayList.length; idx++) {

  var catalogItem = new GlideRecord('sc_cat_item');

  if (catalogItem.get(arrayList[idx])) {

  var task = new GlideRecord('sc_task');

  task.initialize();

  task.request_item = current.sys_id;

  task.short_description = 'My Task for ' + catalogItem.name;

  task.assignment_group.setDisplayValue('MyGroup');

  task.parent = current.sys_id;

  task.state = 'OPEN'; //state is open

  task.insert();

  /*insert one variable on task form

  var myVar = new GlideRecord('sc_item_variables_task');

  myVar.initialize();

  myVar.task = task.sys_id;

  myVar.variable = 'kdjvndv23894u29385';//sys_id for the vairable from item_option_new table

  myVar.insert();

  //end of inserting one variable*/

  //insert all catalog item variables on task form

  var catVariables = new GlideRecord('item_option_new');

  catVariables.addQuery('cat_item',current.cat_item);

  catVariables.query();

  while(catVariables.next()){

  var myVar = new GlideRecord('sc_item_variables_task');

  myVar.initialize();

  myVar.task = task.sys_id;

  myVar.variable = catVariables.sys_id;

  myVar.insert();

  }

  //end of inserting all catalog item variables on task form

  }

  }

}

****************************************************************************************************************************************************************************************************

1 ACCEPTED SOLUTION

rajeshraya
Giga Expert

Discussion already has the answer


View solution in original post

15 REPLIES 15

Hi Shabarish,

 

This script helps you to create variables for each task so that your client scripts or UI policies do not get affected due to unavailability of variables on catalog task. I think they always store the values from the RITM like any other catalog task created through workflow. Hope this explanation helps.

 

Thanks

Rajesh

shabarish1
Kilo Contributor

Hello Rajesh,

 

Yes, variables on task will get data from RITM or from the form submitted.

But we have a requirement to provide certain variables to get data from user task wise.

Like device name, user,date which need to be updated by user. Here is the issue as the variable once updated will be carried to the next generated workflow too.

 

Please share your inputs which you think will help me.

 

Thanks

Shabarish

If you want to filter out some variables based on task, you can check for the name of the variable to be excluded in the while loop below and play accordingly. Hope this helps.

 

//insert all catalog item variables on task form

 

  var catVariables = new GlideRecord('item_option_new');

  catVariables.addQuery('cat_item',current.cat_item);

  catVariables.query();

 

  while(catVariables.next()){

//add some checking here ******************************

if(catVariables.name != "var_name1"){

  var myVar = new GlideRecord('sc_item_variables_task');

  myVar.initialize();

  myVar.task = task.sys_id;

  myVar.variable = catVariables.sys_id;

  myVar.insert();

  }

}

 

  //end of inserting all catalog item variables on task form

Hi Shabarish,

I know it's been a while, but did you ever figure out how to solve this?

Thanks

Chaithra Srini1
Mega Expert

Thank you so much for this post buddy. This really helped me in one of my similar requirements. Thanks a lot!!!