Dynamically create sc_tasks and order their execution

emyrold
Giga Expert

Hello

I have a requirement where I'm having to dynamically create sc_tasks in a wf activity run script and now a new requirement came in about sequencing these tasks.

I've never worked with execution plans and I am wondering if I need to change from having the sc_req_item be workflow-based or part of an execution plan.   I have heard that execution plans have been deprecated in favor of workflows.

Background:

I am doing an Integration between a 3rd party system and ServiceNow.

I have created a "scripted web service" that uses the CART api to dynamically create the sc_request and sc_req_item and does a bunch of other things like setting values in variables and creating the requested_for (user record) if it does not already exist.

There are two custom fields on their Software Asset Mgmt (SAM) table where one is type = list (e.g. watch list) and second is yes/no.   On the List field (provisioning groups) it can contain an empty value or many provisioning groups (upward of 10-15) listed for each SAM record.

At first there was no task ordering/sequencing requirement and for every group in the list, just create a sc_task with the related group becoming the assignment group.   I have this working now.   One group, one sc_task, 13 groups, 13 sc_tasks.

The new requirement now wants to deal with sequencing and has provided the business rules/logic for what the sequencing needs to be.

I have been searching quite a bit on this and saw an old post on SN Guru where Mark had a few options on how to do this (kind of) with execution plans.

I will past the code I have where I do this without the ordering/sequencing part, but am struggling to find a way to do this.   It has been suggested to try the "create task" workflow activity but I'm having a hard time to figure out how to deal with the randomness of it being one or many tasks that need to be created based on how many groups are in the provisioning groups field on the SAM record.

Any idea's or suggestions would be appreciated.

Thanks,

-Erik

// WF Run script to create stasks for IDM Access requests.

//Grab the SAM record from the variable pool of the item.

var samRecSysID = current.variable_pool.sam_record.sys_id;

var samGR = new GlideRecord('cmdb_ci_spkg');   //Base Software Table

samGR.addQuery('sys_id', samRecSysID);

samGR.query();

if (samGR.next()) {

  var list = samGR.u_idm_access_support_groups.getDisplayValue();

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

  // for each support group we need to create a unique task and assign the group to it.

  for(var i=0; i< array.length; i++) {

  var ag = array[i].replace(/^\s+|\s+$/g, "");   //remove leading space after split() for every loop after the first one.

  createStask(ag);

  }

}

// write some code here to set the sc_req_item.order value to help with the sequencing of sc_tasks...???

function createStask(Assignmentgroup) {

  // start task creation

  var taskGR = new GlideRecord('sc_task');

  taskGR.initialize();

  taskGR.request_item = current.sys_id;

  taskGR.short_description = current.short_description;

  taskGR.u_details = current.u_details;

  taskGR.u_due_date = current.u_due_date;

  taskGR.u_op_tier_1 = 'Allocate'; // Temp, need to verify req's

  taskGR.u_op_tier_2 = 'Space'; // Temp, need to verify req's

  taskGR.assignment_group.setDisplayValue(Assignmentgroup);

  taskGR.insert();

}

1 ACCEPTED SOLUTION

brian_quinn
ServiceNow Employee
ServiceNow Employee

Erik,



I think you could accomplish what you are looking for by using a workflow like the one below.   The 3 create task activities for Seq #1/#2/#4 can be set to wait for completion.   The Seq#3 tasks would have to be created using a script, similar to the way you are currently doing it, and a "Wait for condition" script would have to be written to wait for all of those tasks to complete before moving on to Seq #4.  



Workflow.PNG



Thanks


Brian


View solution in original post

4 REPLIES 4

brian_quinn
ServiceNow Employee
ServiceNow Employee

Erik,



What type of sequencing are you trying to implement?   For you example of 13 groups with 13 sc_tasks, would it be tasks 8 and 9 can't be created until tasks 2 and 3 have been completed?   How complex will the sequencing get?



Thanks


Brian


Hi Brian,



So I'm looking at which way to do this so it is less of a hack and will last and the next person coming in can sustain it fairly easily.   So 1) Stub Execution Plan, leveraging the Task Sequencings (execution_plan_local) table   or 2) Some sort of magic in a workflow using the create task activity or scripting that somehow can attach a task to the workflow context.



I believe the Sequencing Requirements are fairly simple.



We have two fields on the software record we will be using:   1. Software Install Support (yes/no) and 2. Support Groups (list).



There are four steps to their sequence (1-4) and it does not matter if all tasks are generated concurrently or sequentially, however if concurrently then Successor Tasks must remain in "Pending" until Predecessor is "Completed".



Seq#1: query the Support group list and if any group = "xzy" then create an "xzy" task with a Seq = 1 and opened.


Seq#2: query the Software Install Support field and if "yes" then dot walk to the requested_for.location.parent.u_support_software_install group field, grab the group, create a task with Seq = 2.   and Pending unless there is no Seq 1 task then it would be Opened.


Seq#3: query the support group list and for every group (besides "xyz" above or EPV below) create one task, all with a Seq = 3. and Pending unless there is no task 1 or 2 then it would be Opened.


Seq#4: query the support group list and if any group = "EPV" then create an "EPV" task with a Seq = 4 and Pending until all 1, 2 or 3 sequenced tasks have been completed or no predecessor tasks.   This must be the last task executed if the "EPV" support group is listed on the software record.



Thanks,



-Erik


brian_quinn
ServiceNow Employee
ServiceNow Employee

Erik,



I think you could accomplish what you are looking for by using a workflow like the one below.   The 3 create task activities for Seq #1/#2/#4 can be set to wait for completion.   The Seq#3 tasks would have to be created using a script, similar to the way you are currently doing it, and a "Wait for condition" script would have to be written to wait for all of those tasks to complete before moving on to Seq #4.  



Workflow.PNG



Thanks


Brian


Wow, thanks Brian....!!!



-e