The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Pass Workflow Scratchpad Variable from a Workflow Run Script to a Flow

Steven Parker
Giga Sage

I currently have a workflow that has a run script in it that kicks off a Flow.  This is working as expected.

 

One question I have though....how can I pass a workflow scratchpad variable to the Flow?  I know the Flow can read the inputs from the Requested Item, but how can you get a workflow scratchpad variable to the Flow?

 

Here is the Run Script in the Workflow that kicks off the Flow.

(function() {
	
	try {
		var inputs = {};
		inputs['request_item'] = current; // GlideRecord of table: sc_req_item 
		inputs['table_name'] = 'sc_req_item';


		// Start Asynchronously: Uncomment to run in background.
		var result = sn_fd.FlowAPI.getRunner().flow('global.outlook_distribution_list').inBackground().withInputs(inputs).run();
				
		// Execute Synchronously: Run in foreground.
		//var result = sn_fd.FlowAPI.getRunner().flow('global.outlook_distribution_list').inForeground().withInputs(inputs).run();

		//gs.info("RESULT OF FLOW VAR:  " + result.contextId);

		//The Sys ID of a flow execution (contextId)
        var ritmcontextId = result.getContextId();
        current.flow_context = ritmcontextId;
        current.update();
		
	} 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()) {

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

	}
		
})();

 


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

Ankur Bawiskar
Tera Patron
Tera Patron

@Steven Parker 

if you are creating a flow then flow can't accept any input

You can create subflow and pass input to that subflow and then use it in your subflow.

Scripting with Flows, Subflows, and Actions 

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

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@Steven Parker 

if you are creating a flow then flow can't accept any input

You can create subflow and pass input to that subflow and then use it in your subflow.

Scripting with Flows, Subflows, and Actions 

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

Thank you Ankur!  That's what I needed....needs to be a subflow versus a flow, set input, copy code snippet and you can add scratchpad variables to the "input" code snippet.

 

One last question....is there a way in the subflow to write back to the RITM that initiated the original Workflow?

 

I was thinking in the Subflow using the "Update Record" and a script within...something like this maybe, but I am not sure trigger has any details in it when it's a subflow:

 
var currentRec = fd_data.trigger.current;
return currentRec;

 

Should I create an "ritm" input on the Subflow and pass "current" to it via the script?  Would that give access to the current record in the subflow


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

I got it Ankur

 

Creating an input variable on the subflow called "ritm" and passing current to it in the standard Workflow run script allowed me to do an "Update Record" in the Subflow and use that ritm input variable as the record to update.

 

Here is the final Code Snippet that is used in a normal Workflow to kickoff the Sub Flow:

(function() {
	
	try {
		var inputs = {};
		inputs['ritm'] = current; // String 
		inputs['username'] = workflow.scratchpad.userName; // String 

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

		gs.info("WORKFLOW OUTPUTS: " + outputs);

		// Current subflow has no outputs defined.		
	} catch (ex) {
		var message = ex.getMessage();
		gs.error(message);
	}
	
})();

 


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

@Steven Parker 

That's correct.

Either pass current object and accept input of type GlideRecord-> RITM and then use "Update Record" or pass sysId and then use Lookup Record on RITM with that sysId and then use "Update Record"

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