Monday, 26 June 2023

Invoking(Launching) a Flow from Apex

 Hi,

We can launch a flow from Apex in the following ways


Invoking the flow from Apex Statically:

Without Namespace:

  Map<String, Object> inputs = new Map<String, Object>();

  inputs.put('AccountID','001XXXXXXX');

  inputs.put('OpportunityID','006XXXXXXX');

  Flow.Interview.Calculate_discounts myFlow = 

  new Flow.Interview.Calculate_discounts(inputs);

  myFlow.start();


With Namespace:

  Map<String, Object> inputs = new Map<String, Object>();

  inputs.put('AccountID','001XXXXXXX');

  inputs.put('OpportunityID','006XXXXXXX');

  Flow.Interview.myNamespace.Calculate_discounts myFlow = 

  new Flow.Interview. sftech.Calculate_discounts(inputs);

  myFlow.start();

Invoking the flow from Apex Dynamically:

Without Namespace:

Map<String, Object> inputs = new Map<String, Object>();

  inputs.put('AccountID','001XXXXXXX');

  inputs.put('OpportunityID','006XXXXXXX');

Flow.Interview myFlow = Flow.Interview.createInterview('Calculate_discounts', inputs);

myFlow.start();

With Namespace:

  Map<String, Object> inputs = new Map<String, Object>();

  inputs.put('AccountID','001XXXXXXX');

  inputs.put('OpportunityID','006XXXXXXX');

  Flow.Interview myFlow = Flow.Interview.createInterview('sftech', 'Calculate_discounts', inputs);

  myFlow.start();

Here Calculate_discounts  is the name of the flow.

AccountID and OpportunityID are input variables of the flow.

sftech is the namespace

How to get variable values from flow to Apex?

system.debug('My Output Variable: ' + myFlow.getVariableValue('varName'));


Note: We can invoke flow from Apex if the flow type is "Autolaunched Flow (No Trigger)" only







Reference:

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/flow_interview_class.htm#apex_Flow_Interview_methods






No comments:

Post a Comment

Grounding Prompt Templates with Apex Merge Fields

 Hi, You can include an Apex merge field in a prompt template to surface data retrieved from a SOQL query or an external API. Apex is also u...