Create multiple tasks via workflow script based on items assigned

YenGar
Mega Sage

Hello,

I have a request to create multiple tasks from a workflow script depending on the items assigned to a user. There is no set amount of tasks that needs to be created, could be 1, 2 or 3, depends on the assets assigned.   I am having a hard time querying the items assigned to the user to be able to create a task for each one.

Basically, I am querying a table (Master) to see how many master records are assigned to the user and then create a task   per record found. An Assets table is referenced form the Master table which contains the assignment group that the tasks need to be assigned to.

My goal is to query the Master table, identify how many master records are assigned to the user, and create tasks for each master record found for the user and assign it to the group in the assets table.  

This is what I've been working on but this isn't working as expected, it doesn't create the tasks...

Workflow script:

var assnt_group = [];

var RequestedFor = current.variables.requested_for;

var gr =   new GlideRecord('u_nerc_cip_access');

gr.addQuery('u_contact_name', RequestedFor);

gr.query();

var grps = 0;

while(gr.next()) {

  assnt_group.push(gr.u_asset.u_contractor_pra_contact_group + "");

}

//Then you can create a task for each assignment group

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

try{

var tsk = new GlideRecord('sc_task');

tsk.initialize();

tsk.u_access_requested_for = current.variables.u_term_name;

    tsk.due_date = current.due_date;

    tsk.request_item = current.sys_id;

    tsk.parent = current.sys_id;

    //tsk.assignment_group = acc.u_asset.u_contractor_pra_contact_group;

    tsk.short_description = "Enter Contractor / Vendor's Valid Background Check Date";

    tsk.description = "1.Enter the contractor / vendor's valid NERC-CIP background check date. (This may require contacting the vendor to attest to a new background check if the current one is older than 6.5 years.)"+"\n";

    tsk.description += "2.Close the Task.";

    tsk.state = 1;

    tsk.u_bg = 'true';

    tsk.u_business_reason = current.variables.u_business_reason;

  task.assignment_group =   assnt_group[i];

  task.insert();

} catch(e) {

gs.log("Exception in task creation " + e);

}

}

Any help is truly appreciated!

Thank you,

Yeny

1 ACCEPTED SOLUTION

You defined the object and called it 'arr', but later tried to use it as arrayUtil.



Change them all to one or other and you should be fine.


View solution in original post

15 REPLIES 15

Hi Yeny,



Well if you got it to the point where it's not throwing any syntax errors in scripts background, that's great.



Running in scripts background using the 'faux current' method I described earlier, you can put in statements like



gs.log('processing ' + assnt_group.length + ' records');



before your for loop to validate the length of that array and see how many you are actually supposed to create. Or, put a line just inside the for loop



gs.log('i=' + i);



to ensure you are moving through the records. That's an example to diagnose your script as it runs. Offhand, I don't see anything obvious that would preclude the tasks from being created unless it never gets in that for loop.


Hi Chuck,



Thank you for your help! I got it to create the tasks that needed to be created!



I have another question though... How can I get it to create only one task for an assignment group if the assignment group is the same for different assets. For example, a user has 3 assets assigned to them. 2 assets have the same assignment group and the 3rd   one has a different assignment group. I would need to create 2 tasks instead of 3 as the workflow is currently doing. Since all the assignment group values found are being put into an array, how can I filter out 'duplicate' assignment groups?



Thank you,


Yeny


To remove duplicates, use the ArrayUtil().unique() method.



ArrayUtil - ServiceNow Wiki


Thank you Chuck!!



I tried that several times but I keep getting an error in the 'Run Script' activity in the workflow. The error says 'arrayUtil' is not defined.



below are the changes that I made...   from what I can see, this should work... not sure what I am missing?



var RequestedFor = current.variables.requested_for+'';


var active = "Active";


var arr = new ArrayUtil();


var assnt_group = [];


var uniquegrp = [];


var gr =   new GlideRecord('u_nerc_cip_access');


gr.addQuery('u_contact_name', RequestedFor);


gr.addQuery('u_access_status', active);


gr.query();




while(gr.next()) {


  //collect groups into array


  assnt_group.push(gr.u_asset.u_contractor_pra_contact_group);


}


uniquegrp = arrayUtil.unique(assnt_group);


//Then you can create a task for each assignment group


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


//try{


//if(assnt_group.length>0){


var tsk = new GlideRecord('sc_task');


tsk.initialize();


tsk.u_access_requested_for = current.variables.u_term_name;


    tsk.due_date = current.due_date;


    tsk.request_item = current.sys_id;


    tsk.parent = current.sys_id;


    //tsk.assignment_group = acc.u_asset.u_contractor_pra_contact_group;


    tsk.short_description = "Enter Contractor / Vendor's Valid Background Check Date";


    tsk.description = "1.Enter the contractor / vendor's valid NERC-CIP background check date. (This may require contacting the vendor to attest to a new background check if the current one is older than 6.5 years.)"+"\n";


    tsk.description += "2.Close the Task.";


    tsk.state = 1;


    tsk.u_bg = 'true';


    tsk.u_business_reason = current.variables.u_business_reason;


  tsk.assignment_group = uniquegrp[i];


  tsk.insert();


  //catch(e) {


//gs.log("Exception in task creation " + e);


}


//}


You defined the object and called it 'arr', but later tried to use it as arrayUtil.



Change them all to one or other and you should be fine.