Passing array of values in scratchpad workflow and calling the subflow for each element in array
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2023 01:26 AM
Hi Experts,
Please help me in storing an array of values through workflow scratchpad and passing it to subflow via workflow activity.
Here is my code
var catalog_workflow_group;
var list =current.variables.access_type.toString();
gs.log('hcv: ' + list);
var listSplit = list.split(',');
gs.log('listsplit: ' + listSplit);
for (var i = 0; i < listSplit.length; i++) {
var softwareRef = new GlideRecord('u_active_directory_software_reference');
softwareRef.addQuery('sys_id', listSplit[i].trim());
softwareRef.query();
while (softwareRef.next()) {
workflow.scratchpad.catalog_workflow_group=softwareRef.u_active_directory_group.toString();
}
gs.log('Answer sc is: ' +workflow.scratchpad.catalog_workflow_group);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2023 01:57 AM
@Hemachithra The following piece of code is not going to yield the expected results.
while (softwareRef.next()) {
workflow.scratchpad.catalog_workflow_group=softwareRef.u_active_directory_group.toString();
}
as the scratchpad variable catalog_workflow_group will be overriden by the values from softwareRef each time the loop runs.
Instead you should update your code as follows.
var catalog_workflow_group;
var list =current.variables.access_type.toString();
gs.log('hcv: ' + list);
var listSplit = list.split(',');
gs.log('listsplit: ' + listSplit);
var activeDirectoryGroup=[];
for (var i = 0; i < listSplit.length; i++) {
var softwareRef = new GlideRecord('u_active_directory_software_reference');
softwareRef.addQuery('sys_id', listSplit[i].trim());
softwareRef.query();
while (softwareRef.next()) {
activeDirectoryGroup.push(softwareRef.u_active_directory_group.toString());
}
}
if(activeDirectoryGroup.length>0){
workflow.scratchpad.catalog_workflow_group = activeDirectoryGroup;
gs.log('Answer sc is: ' +workflow.scratchpad.catalog_workflow_group);
}
Hope this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2023 03:56 AM
Hi ,
Thanks for help the code is returning desired sys ID, but in subflow the array is passing only one sys ID eventhough im getting desired sys id's in logs.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2023 09:21 AM
Hi Sandeep,
The above code returns the following sys id correctly but the subflow following this activity is triggered only once it should trigger twice as two sys id is generated.
here is the subflow and parameters passed in subflow.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-23-2023 02:16 AM
Hi @Hemachithra
To pass the value from main flow to sub-flow , you need to create 'Input' variable in sub-flow -
For example , here I have created "sys id" variable
Now in the Main workflow , add that workflow & set the variable with "scratchpad" value
Set the scratchpad value
Note :
If your working running on "sc_req_item" then you need to enable the below workflow property to add input
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates