- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2020 12:22 PM
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2020 12:25 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2020 06:13 AM
You are very welcome! Happy to help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2020 01:01 PM
Michael, if you would not mind I have another question on the topic. I am trying just a simple loop to see how this is going to work and basically just writing information to the system logs for each step so I can see it's working as it should. I have included the code and am working off of a List Type field on the form. *Note if it is Yes on the If, end else go back
See graphic
the first run script does the following
// Set the scratchpad counter to zero
workflow.scratchpad.companyCounter = 0;
//Create Array with company guids on it
var compList = current.u_company_siem_list.toString().split(",");
//get the length of the list and put it into a variable
//var compListLength = compList.length;
workflow.scratchpad.compListLength = compList.length;
//log the company stuff in the log files
gs.info("Test1 - Companies in the compList are: " + workflow.scratchpad.compListLength);
Second Run Script - just sets the workflow scratchpad to the first company list
--NOTE--
Steps 1 and 2 are good, the log files are printing what they need to
workflow.scratchpad.currentComp = compList[workflow.scratchpad.companyCounter];
gs.info("Test1 - The current company is: " + workflow.scratchpad.currentComp);
Then The next step is where i have the question and things get wonky, I'm basically (at least I think I am) looking to see if the counter is greater then the length of the list so if it is not then go back to step 2 and print the second company name but it's looping
answer = ifScript();
function ifScript() {
if (workflow.scratchpad.currentCounter > workflow.scratchpad.compListLength) {
gs.info("TESTSIEMLOOP - " + workflow.scratchpad.currentComp + " : Yes ");
gs.info("Test1 - The current lenght is: " + workflow.scratchpad.compListLength);
return 'yes';
}
gs.info("TESTSIEMLOOP - " + workflow.scratchpad.currentComp + " : No");
return 'no';
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2020 08:30 AM
Steve at first glance your last activity is referencing "currentCounter" which should be "companyCounter". Regardless I wouldn't spend a lot of time with this test and instead focus on how you plan to create these changes. Have you thought about that yet? You could do this via a single run script where you loop through your list in a for loop and create the changes. Or if you really want to loop through via workflow activity loop that will be a bit different.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2020 01:45 PM
So, when you say a list, what do you mean exactly, a variable of type GlideList? I don't think I've ever quite seen a value like {"complist":"4d8a6dce4c900600fdf9cbd259a26c72,1ccc2c441bc8045076fadb1dcd4bcbcb,7aad306a3dd2f100fdf95d7a04b44117"} before.
Here are a few things that I would try (which align with what was mentioned above, with some minor differences).
workflow.scratchpad.companyList = current.field_name_im_using.getValue().split(',');
For any list type I know of that statement will result in workflow.scratchpad.companyList being set as an array of sys_id's. You loop it using something like:
for(var i = 0; i < workflow.scratchpad.companyList.length; i++) {
//just logs the array
gs.info(workflow.scratchpad.companyList[i]);
}
Now, on that note, do you really want the sys_id's of the records or are you just going to convert those to the name of the records? If so, you can skip a step with a slight adjustment to:
workflow.scratchpad.companyList = current.field_name_im_using.getDisplayValue().split(',');
That will give you the display values, also in an array.
Still, I'd be interested in what kind of field gives you that value...
If this was helpful or correct, please be kind and remember to click appropriately!
Michael Jones - Proud member of the CloudPires team!
Michael D. Jones
Proud member of the GlideFast Consulting Team!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2020 05:40 AM
Thank you for the information it was very helpful, I appreciate all of the help guys