+ Start a Discussion
Jenna HildebrandJenna Hildebrand 

Call and Pass Variables to Flow via Apex Class/Trigger


I've created a flow that updates an opportunity text field with the names of the products associated with that opportunity (separated by semicolons). This field should be updated whenever a new product is added or deleted, so I need to be able to invoke this flow with a trigger rather than just process builder. I've looked at a lot of different resources, but I can't seem to find something that explains how the apex class and trigger work together to call the flow and pass variables from the trigger/class to the flow. Would seriously appreciate any guidance or code snippet anyone could offer!

SandhyaSandhya (Salesforce Developers) 

Check below link which has sample code that can help you.

Best Regards,
Raj VakatiRaj Vakati
From Apex Class

You can retrieve flow variables for a specific flow in Apex.

The Flow.Interview Apex class provides the getVariableValue method for retrieving a flow variable, which can be in the flow embedded in the Visualforce page, or in a separate flow that is called by a subflow element. This example shows how to use this method to obtain breadcrumb (navigation) information from the flow embedded in the Visualforce page. If that flow contains subflow elements, and each of the referenced flows also contains a vaBreadCrumb variable, the Visualforce page can provide users with breadcrumbs regardless of which flow the interview is running.
public class SampleContoller {

   // Instance of the flow
   public Flow.Interview.Flow_Template_Gallery myFlow {get; set;}

   public String getBreadCrumb() {
      String aBreadCrumb;
      if (myFlow==null) { return 'Home';}
      else aBreadCrumb = (String) myFlow.getVariableValue('vaBreadCrumb');

      return(aBreadCrumb==null ? 'Home': aBreadCrumb);


Refer this links




Jenna HildebrandJenna Hildebrand

Thank you both for the input! Does the below class make sense? I'm getting the following compile error when I try to save it:

Error: Compile Error: Expecting ')' but was: 'varOpportunityId' at line 11 column 19

public class OpportunityProductFlowController {

    // get value of OpportunityId from OpportunityLineItem to pass to flow
    public String returnOpportunityId(OpportunityLineItem[] oli) {
        String opportunityId = [SELECT OpportunityId FROM OpportunityLineItem WHERE Id = :oli].OpportunityId;
        System.debug('Opportunity Id: ' + opportunityId);
        return opportunityId;
    // pass opportunityId value to flow variable 'varOpportunityId' and call flow
    Map<String, Object> variables = new Map<String, Object>();
    variables.put('varOpportunityId', opportunityId);
    myFlow = new Flow.Interview.Update_Products_Field_on_Opportunity(variables);
Also, below is the trigger. I'd like for this to fire whenever an OpportunityLineItem is created or deleted. Are my events correct?
trigger OpportunityProductsTrigger on OpportunityLineItem (after insert, before delete) {

    OpportunityLineItem[] oli = Trigger.new;