Passing array of values in scratchpad workflow and calling the subflow for each element in array

Hemachithra
Tera Contributor

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);
}

 

 

7 REPLIES 7

Sandeep Rajput
Tera Patron
Tera Patron

@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.

 

 

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.

Hemachithra_1-1698058450092.png

 

 

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.

Hemachithra_3-1698078071864.png

here is the subflow and parameters passed in subflow.

 

Hemachithra_2-1698077951834.png

 



Vishal Birajdar
Giga Sage

Hi @Hemachithra 

 

To pass the value from main flow to sub-flow , you need to create 'Input' variable in sub-flow - 

 

VishalBirajdar_0-1698052172116.png

 

For example , here I have created "sys id" variable

 

VishalBirajdar_1-1698052224028.png

 

 

Now in the Main workflow , add that workflow & set the variable with "scratchpad" value

 

VishalBirajdar_2-1698052351754.png

 

Set the scratchpad value

 

VishalBirajdar_3-1698052383730.png

 

 

Note : 

If your working running on "sc_req_item" then you need to enable the below workflow property to add input

 

VishalBirajdar_4-1698052547240.png

 

 

 

 

 

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates