passing an array to a workflow.scratchpad

poyntzj
Kilo Sage

I have a workflow that is used as a sub workflow.

I normally pass a number of variables from the instigating workflow to the sub workflow.

All nice and easy as so far it is just a single value each time.

 

An extra requirement has now been made and   I may need to pass a number of values in this one variable to the subworkflow.

On looking at the inputs i can see there is no "Array" so I may have to use string and seperator, however I do see an option of "List"

how would I

  • populate it to send to the sub workflow
  • get the sub workflow to read the list

 

Cheers

1 ACCEPTED SOLUTION

Had a little look into this now and i have going to send the information over as a string and then manipulate in the string in the sub workflow



my test script (looks at a change with a number of change tasks), it gets all the assignment groups and adds to a string.


I take that string, split and then get all the unique values before looking at what the query will do




//section for parent workflow


var strCtask = '';


var gr = new GlideRecord('change_task');


gr.addQuery('change_request','5cc3fd766fff5dc063e24c871e3ee423');


gr.query()


while (gr.next())


{


      strCtask += gr.assignment_group + '|'


}



gs.print(strCtask);




// section of sub workflow


if (strCtask != '')


{


      var inSplit = strCtask.split("|");


      var procArr = new ArrayUtil().unique(inSplit);


}



var aaggr = new GlideRecord('u_advanced_approval_groups')



if (procArr.length != undefined)


{


      if (procArr.length == 1)


              aaggr.addQuery('u_change_task_assignment_group','CONTAINS',procArr[0]);


      else if (procArr.length > 1)


      {


              oaaggr = aaggr.addQuery('u_change_task_assignment_group','CONTAINS',procArr[0]);


              for (var i=1; i < procArr.length ; i++)


              {


                      if (procArr[i] != '')


                              oaaggr.addOrCondition('u_change_task_assignment_group','CONTAINS',procArr[i]);


              }


      }


}



aaggr.query();


while (aaggr.next())


{


gs.print(aaggr.u_name);


}



View solution in original post

6 REPLIES 6

ohhgr
Kilo Sage
Kilo Sage

Hi Julian,



Check if you could use the JSON Script Include provided by ServiceNow.



Thanks,
Mandar


Mark Laucus
Giga Guru

As Mandar Kamtekar stated above the way to move array information is to use a JSON object.   Here is how I did it.



I have a run script object that will create the initial JSON script in the main workflow



Parent workflow

// This script will create an object and populate it with initial data to pass between workflow


// Creating initial object and populating data



var obj = {}; //create an object


obj['val1'] = "false";


obj['val2'] = "false";


obj['val3'] = "false";



var json = new JSON(); //create JSON object



// encode the values from the object and pass it as and OUTPUT variable


workflow.scratchpad.u_approval_response = json.encode(obj);



Then I will use the scratchpad variable to pass in between workflows



Passing Variable


${workflow.scratchpad.u_approval_response}


In my sub workflows I will deconstruct the object into variables to use




Sub Workflow

//decode passed variable which will recreate an object



var subapprove = workflow.inputs.u_approval_response_subapprove;



var json = new JSON();


var obj2 = json.decode(subapprove);




//extract values to your local variables


workflow.scratchpad.answer1 = obj2['val1'];


workflow.scratchpad.answer2 = obj2['val2'];


workflow.scratchpad.answer3 = obj2['val3'];



Hopefully this provides an idea how to do it.


I shall look and play on Monday and determine if using an array or sring is better.



I am thinking string so I can send over


workflow.u_passed_string = '\"option 1\",\"option 2\", \"option 3\"'



and then in the subworkflow



gr.addQuery('field to search",'IN',${workflow.u_passed_string});


Had a little look into this now and i have going to send the information over as a string and then manipulate in the string in the sub workflow



my test script (looks at a change with a number of change tasks), it gets all the assignment groups and adds to a string.


I take that string, split and then get all the unique values before looking at what the query will do




//section for parent workflow


var strCtask = '';


var gr = new GlideRecord('change_task');


gr.addQuery('change_request','5cc3fd766fff5dc063e24c871e3ee423');


gr.query()


while (gr.next())


{


      strCtask += gr.assignment_group + '|'


}



gs.print(strCtask);




// section of sub workflow


if (strCtask != '')


{


      var inSplit = strCtask.split("|");


      var procArr = new ArrayUtil().unique(inSplit);


}



var aaggr = new GlideRecord('u_advanced_approval_groups')



if (procArr.length != undefined)


{


      if (procArr.length == 1)


              aaggr.addQuery('u_change_task_assignment_group','CONTAINS',procArr[0]);


      else if (procArr.length > 1)


      {


              oaaggr = aaggr.addQuery('u_change_task_assignment_group','CONTAINS',procArr[0]);


              for (var i=1; i < procArr.length ; i++)


              {


                      if (procArr[i] != '')


                              oaaggr.addOrCondition('u_change_task_assignment_group','CONTAINS',procArr[i]);


              }


      }


}



aaggr.query();


while (aaggr.next())


{


gs.print(aaggr.u_name);


}