How to run a loop to create dynamic catalog task in workflow?

ssc
Kilo Explorer

Please tell me how to run the below in a loop:

I have 15 checkboxes in my catalog item. If 3 checkboxes are checked then 3 catalog task should get created through workflow, if all 15 checkboxes are checked then 15 catalog tasks should get created and each task will be assigned to different assignment group. In order to fulfill this, I am creating tasks dynamically through workflow. In my workflow I have added Run script activity and tried to create it through script. Please find the snippet of code below.  

var tsk = new GlideRecord('sc_task');

tsk.initialize(); tsk.request_item = current.sys_id; //assigning to current request item.

tsk.assignment_group = '2bc813760a0a3c2b003a6945582eed1d'; //sys_id of some group.

tsk.short_description = 'test';

tsk.state = 1; //state is open

tsk.insert();

What will be the logic to assign 10 tasks to 10 different groups(I am using a custom table currently;can I fetch it from there??)

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Shevangi,



First of all you need to know for each checkbox which assignment group to be populated in individual task.


Sample code:


first of all store the mapping of the checkbox with assignment group sys_id using json, easiest way would be to have key value pair


key is the variable name of checkbox and value is assignment group sys_id


{"variable1":"2bc813760a0a3c2b003a6945582eed1d","variable2":"2bc813760a0a3c2b003a6945582eed12"}



have if else condition and pick the value of assignment group by parsing the json above



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thanks @ankur bawiskar! I am trying this,


could you pls tell me how to run a loop for creating dynamic task because currently its only creating one task at a time from my code.


var tsk = new GlideRecord('sc_task');


tsk.initialize(); tsk.request_item = current.sys_id; //assigning to current request item.


tsk.assignment_group = '2bc813760a0a3c2b003a6945582eed1d'; //sys_id of some group.


tsk.short_description = 'test';


tsk.state = 1; //state is open


tsk.insert();


Hi Shevangi,




I have one solution which is implemented:



You have to check each checkbox and if it is true then you have to call a function which will create task for you.



See below:



if(current.variables.checkbox_name == true)


{


        createTask('Sys_Id of Group','Short Description');


}




function createTask(id,short)


{


var tsk = new GlideRecord('sc_task');


tsk.initialize(); tsk.request_item = current.sys_id; //assigning to current request item.


tsk.assignment_group = id; //sys_id of some group.


tsk.short_description = short;


tsk.state = 1; //state is open


tsk.insert();


}



Thanks,