Help adding a loop in workflow

Steve42
Tera Expert

I'm trying to create a workflow that will take a list and put it into an array, then loop through the workflow so it will create the change requests for each company in the list.

I have tried just using workflow.scratchpad.company = <field_on_form_name> and I get back the object.  If I look at background scripts and pull the list It is an object, my results are as follows
Which is a string
{"complist":"4d8a6dce4c900600fdf9cbd259a26c72,1ccc2c441bc8045076fadb1dcd4bcbcb,7aad306a3dd2f100fdf95d7a04b44117"}

I need to put these sys_id's into a array to be able to loop through them but not understanding how to do that.  Here is what I have tried

workflow.scratchpad.companyList = current.field_name_im_using

var myCompanyList = [];

myCompanyList.push(workflow.scratchpad.companyList);

 

While I understand that this is probably totally wrong, I could use some help  So here is what i need,  If someone could answer the following:

1.  Am I doing the Array correct?
2.  How can I get the l take the string that workflow.scratchpad.name = current.field_name_im_trying_to_pull
3.  Setup the counter.

This is literally the last thing in the workflow I need to accomplish and should not be this hard

1 ACCEPTED SOLUTION

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

When you have a comma separated string you can use the split() function to turn that into an array:

var myCompanyList = current.field_name_im_using.toString().split(",");

Just remember that workflow scratchpad variables are stored as strings so each time you will need to transform the scratchpad value into an array.  Then to get the length of the array use the length function

var myCompanyListLength = myCompanyList.length

Counter would just be workflow.scratchpad.companyCounter = 0 and then to iterate:

workflow.scratchpad.companyCounter += 1;

You can then use the counter to get the element of the array by:

workflow.scratchpad.companyList = current.field_name_im_using
var myCompanyList = workflow.scratchpad.companyList.toString().split(",");
var currentCompany = myCompanyList[workflow.scratchpad.companyCounter];

Also keep in mind array's are ordered starting with 0, then 1, then 2, etc.  So make sure your counter starts as 0 otherwise you will skip the first entry.

 

View solution in original post

9 REPLIES 9

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

When you have a comma separated string you can use the split() function to turn that into an array:

var myCompanyList = current.field_name_im_using.toString().split(",");

Just remember that workflow scratchpad variables are stored as strings so each time you will need to transform the scratchpad value into an array.  Then to get the length of the array use the length function

var myCompanyListLength = myCompanyList.length

Counter would just be workflow.scratchpad.companyCounter = 0 and then to iterate:

workflow.scratchpad.companyCounter += 1;

You can then use the counter to get the element of the array by:

workflow.scratchpad.companyList = current.field_name_im_using
var myCompanyList = workflow.scratchpad.companyList.toString().split(",");
var currentCompany = myCompanyList[workflow.scratchpad.companyCounter];

Also keep in mind array's are ordered starting with 0, then 1, then 2, etc.  So make sure your counter starts as 0 otherwise you will skip the first entry.

 

You can store an array in a workflow scratchpad variable then use it later. 

Just create the variable with workflow.scratchpad.whatevername = [] then later use workflow.scratchpad.whatevername.push(your value to push into the array)

Then later in the workflow you can use it by doing something like var whateverArray = workflow.scratchpad.whatevername;

Hum I know in the past it was translated back to a string.  Good to know.

Michael, thank you a bunch on this, I really appreciate it a lot, Very helpful information in this post from you.  Kudo's