Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Passing data to a Sub Flow Input of Array.String type

Steven Parker
Giga Sage

So I have a Sub Flow with an input variable of type Array.String and it's named 'pcname'.  If I test the flow and just type a name in there it works fine.  However, when I use the Code Snippet and try to pass a string variable (converted to an array) to that Sub Flow input, it errors out.  

 

How do I pass data to the 'pcname' "Array.String" input variable on the Sub Flow?  I am just passing 1 variable for now.

 

StevenParker_0-1758914923152.png

 


Please mark this response as correct and/or helpful if it assisted you with your question.
Steven
10 REPLIES 10

Left of the image is the catalog item run and the right is my manual "TEST" run....no difference.

 

StevenParker_4-1758918605916.png

 


Please mark this response as correct and/or helpful if it assisted you with your question.
Steven

Brad Bowman
Kilo Patron
Kilo Patron

As suggested in the script editor when first expanding it, you need to return something, and the 'current.variables' convention used everywhere else in ServiceNow is a foreign concept to Flow Designer.  So in a simplified example when you add a Subflow action and toggle the script, a one line script would be similar to 

return fd_data.trigger.request_item.variables.computer_name;

when you type fd_data. you'll see a prompt for the next available option, then even though 'variables' doesn't prompt, it works.  Once you can get a Catalog Item variable passed into a Subflow string input, you would use the same syntax in your script pushing that variable to an array, etc.  You drag a data pill or write a script for each subflow input individually, so your try block is not needed.

 

I am working in a workflow and using a Run Script to call a sub flow


Please mark this response as correct and/or helpful if it assisted you with your question.
Steven

Steven Parker
Giga Sage

@Ankur Bawiskar 

 

Ankur do you have an explanation for this?  If I run this flow via a run script, I get this "invalid return type" every time and all I am passing are string fields...text fields.

 

StevenParker_0-1759175469520.png

 

But when I test manually with the same exact information that I just type in, it works fine:

StevenParker_1-1759175513040.png

 

Here is the run script in the workflow that is kicking off the sub flow.  I did exactly what ServiceNow suggested in this KB around converting the input to string.  

 

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0832622

I have also tried:

inputs['pcname'] = current.variables.computer_name; // String
and
inputs['pcname'] = current.variables.computer_name.toString(); // String
(function() {
	
	try {
		var inputs = {};
		inputs['ritm'] = current; // String 
		inputs['pcname'] = '' + current.variables.computer_name; // String 
		inputs['groupid'] = current.variables.group_id; // String 

		// Start Asynchronously: Uncomment to run in background. Code snippet will not have access to outputs.
		// sn_fd.FlowAPI.getRunner().subflow('global.copy_of_add_pc_to_ap_dvc_base_entra_group').inBackground().withInputs(inputs).run();
				
		// Execute Synchronously: Run in foreground. Code snippet has access to outputs.
		var result = sn_fd.FlowAPI.getRunner().subflow('global.copy_of_add_pc_to_ap_dvc_base_entra_group').inForeground().withInputs(inputs).run();
		var outputs = result.getOutputs();

		// Get Outputs:
		// Note: outputs can only be retrieved when executing synchronously.
		var deviceaddoutput = outputs['deviceaddoutput']; // String
		gs.info("DEVICE ADD OUTPUT: " + deviceaddoutput);
		
	} catch (ex) {
		var message = ex.getMessage();
		gs.error(message);
	}
	
	var contextId = result.getContextId(); //get flow context sys_id

	var fc = new GlideRecord('sys_flow_context'); //query the table sys_flow_context using the flow context sys_id to get the sys_flow_context record and populate the fields source_table and source_record
	fc.addQuery('sys_id', contextId);
	fc.query();
	while (fc.next()) {
		gs.info("INSIDE THE WHILE STATEMENT");
	fc.setValue('source_table', 'sc_req_item');
	fc.setValue('source_record', current.sys_id); //sys_id of the RITM on which you want to start the flow
	fc.update();

	}
})();

 

This is so frustrating....It just seems like Sub Flows don't do a very good job at handling string inputs.


Please mark this response as correct and/or helpful if it assisted you with your question.
Steven

@Steven Parker 

yes it's confusing when it comes to subflow/actions with regards to the input types and output variables

so you want to check the value passed is present in that array or not?

share your subflow input type screenshots.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader