Scripting for multiple tasks in workflow

davidwilhelm
Kilo Contributor

Greetings everyone.

First I'd like to say that I'm not saying this is the best way to do this and I look forward to any comments that would improve this process.

Our old Terminations workflow process looked like the below image. Notice the multiple Catalog Task items. This way of creating multiple tasks for one requested item is cumbersome. Not only that, if something core had to be changed, each of the Catalog Tasks had to be manually modified.

Capture.PNG

I recently recreated this and thought I would share what I came up with. First off, I created a custom table that allows a particular set of people based on a role the ability to maintain our Termination Requests task data. This table and module allows end users the ability to make changes without change requests to modify the workflow.

I then recreated the workflow using a single Run Script control.

Capture1.PNG

Below is the script I created in the Run Script control:

var grTerminationTasks = new GlideRecord('u_termination_tasks');

if(current.variable_pool.u_item_sox == 'Yes'){

        grTerminationTasks.addQuery('u_sox', true);

}else{

        grTerminationTasks.addQuery('u_sox', false);

}

var empName;

if (current.variable_pool.employee_type.getDisplayValue() == 'New Employee'){

        empName = current.variable_pool.new_employee.getDisplayValue();

}

else

        empName = current.variable_pool.requested_for.getDisplayValue();

grTerminationTasks.query();

while(grTerminationTasks.next())

{

        var grTask = new GlideRecord('sc_task');

        grTask.intialize();

        grTask.u_category = grTerminationTasks.u_category;

        grTask.cmdb_ci = grTerminationTasks.u_configuration_item;

        grTask.assignment_group = grTerminationTasks.u_assignment_group;

        grTask.assigned_to = grTerminationTasks.u_assigned_to;

        grTask.short_description = 'Termination Request: ' + grTerminationTasks.getDisplayValue('u_short_description') + ' for '   + empName + '. Termination Date: ' + current.variable_pool.terminated_date.getDisplayValue();

        var desc = 'Termination Request: ' + grTerminationTasks.getDisplayValue('u_short_description') + ' for '   + empName + '.';

        desc += '\nTermination Date: ' + current.variable_pool.terminated_date.getDisplayValue() + '\nDue Date: ';

        if(grTerminationTasks.getValue('u_date_type') == '2'){

                  desc += current.variable_pool.two_day_completion_date;

                  grTask.due_date = current.variable_pool.two_day_completion_date;

                  grTask.priority = '1';

        }

        else if (grTerminationTasks.getValue('u_date_type') == 7){

                  desc += current.variable_pool.mandatory_completion_date;

                  grTask.due_date = current.variable_pool.mandatory_completion_date;

                  grTask.priority = '2';

        }

        else if (grTerminationTasks.getValue('u_date_type') == '28'){

                  desc += current.variable_pool.twenty_eight_day_completion_date;

        grTask.due_date = current.variable_pool.twenty_eight_day_completion_date;

        grTask.priority = '4';

        }

        grTask.description = desc;

        grTask.request_item = current.sys_id;

        grTask.parent =   current.sys_id;

        grTask.approval = 'approved';

        grTask.insert();

}

3 REPLIES 3

David Ramirez
Kilo Guru

 

This is a great improvement, in my experience, there are a couple of suggestions for you to look at:

  1. You may need to add business rules on the catalog tasks to "nudge" the workflow, this is needed because under certain conditions the workflow may not be aware to changes in the tasks that were created via script, e.g. task being deleted, closed, canceled, etc and you may end up with a broken workflow instance.
  2. If your users *really miss* having variable editor with all the variables you may want to look into using your script to artificially add the variables, this can be achieved by copying the RITM variables from the item variables table [sc_item_option_mtom] to the Task variables table [sc_item_variables_task]  (I may post an article on this soon)

David

 

P.S.  Script for nudging the workflow may look like this:

 

var gr = new GlideRecord('sc_req_item');		
if (gr.get(current.request_item);) {
     new Workflow().broadcastEventToCurrentsContexts(gr, 'update', null);
}

Daniel Oderbolz
Kilo Sage

Dear David


Thanks a lot for this valuable contribution!
I wonder about your second point (copying the variables to the task).
Do you have code that works?
Does the variable editor in the sc_task then look like the one in sc_req_item?
(I just wrote an enhancement request because IMHO, the "Create Catalog Task" Activiy should support this OOTB).

Thanks & Best
Daniel

 


If this answer was helpful, I would appreciate if you marked it as such - thanks!

Best
Daniel

Variable editor in sc_task is same as one on RITM. If you do not have it on form, consider adding it from form layout. 

 

With respect to variables on sc_task, this is one of the scripts that you can use to copy the variables to sc_task from RITM.

https://community.servicenow.com/community?id=community_blog&sys_id=fabc2e25dbd0dbc01dcaf3231f9619e5