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

I wanted to share the code to include variables on catalog tasks created by gliding sc_task table. the question itself has the answer folks.


I wanted to share the code to include variables on catalog tasks created by gliding sc_task table. the question itself has the answer folks.


jnovack
Kilo Guru

To iterate through all the Variable SETS, you have to go a few more levels deeper.



//start - insert all catalog item variable-set variables on task form


var variableSets = new GlideRecord('io_set_item');


workflow.info('current.cat_item = ' + current.cat_item);


variableSets.addQuery('sc_cat_item',current.cat_item);


variableSets.query(); // looking for io_set_item.variable_set == item_option_new_set.sys_id




while(variableSets.next()){   // iterate through variable sets on catalog item


        var setVariables = new GlideRecord('item_option_new_set');


        workflow.info('variableSets.variable_set = ' + variableSets.variable_set);


        setVariables.addQuery('sys_id',variableSets.variable_set);


        setVariables.query();



        while(setVariables.next()) { // iterate through each variable set


                  var catsetVariables = new GlideRecord('item_option_new');


                  workflow.info('setVariables.sys_id = ' + setVariables.sys_id);


                  catsetVariables.addQuery('variable_set',setVariables.sys_id);


                  catsetVariables.addQuery('type', 'NOT IN', '280d44163720300054b6a3549dbe5d3c,ec0d44163720300054b6a3549dbe5d3c,311dc651c3121100c8b837659bba8fc4,ac0d44163720300054b6a3549dbe5d3c');


                  // Type is NOT (Break, Container End, Container Split, Container, Start)


                  catsetVariables.query();



                  while(catsetVariables.next()) { // iterate through variables in variable set


                            var mysetVar = new GlideRecord('sc_item_variables_task');


                            mysetVar.initialize();


                            workflow.info('taskRec.sys_id = ' + taskRec.sys_id);


                            mysetVar.task = taskRec.sys_id;


                            workflow.info('catsetVariables.sys_id = ' + catsetVariables.sys_id);


                            mysetVar.variable = catsetVariables.sys_id;


                            mysetVar.insert();


                  }


        }


}


//end - insert all catalog item variable-set variables on task form


Thank you for enhancing this Justin. Appreciate it.


shabarish1
Kilo Contributor

Hello Rajesh,

 

I too have the similar requirement, but the problem is when we try to create multiple tasks and insert the same form variables, then the same variables will be used right.

Let us consider in the workflow generated using run scripts, consists of task-A and task-B

variables are

- task_a_var in task-A and

- task_b_var in task-B

Now, if the workflow is invoked for the second or third time on the same request item then the variables will already be with some data filled in by tasks generated in the workflow invoked for the first time.

Can you provide any any suggestions to overcome this?