function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jenna HildebrandJenna Hildebrand 

Call and Pass Variables to Flow via Apex Class/Trigger

Hi,

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!

Thanks!
SandhyaSandhya (Salesforce Developers) 
Hi,

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

https://andyinthecloud.com/2014/10/26/calling-flow-from-apex/
 
Best Regards,
Sandhya
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

https://andyinthecloud.com/2014/10/26/calling-flow-from-apex/

https://salesforce.stackexchange.com/questions/205654/flow-calling-apex-cant-pass-collection-variable
https://salesforcesidekick.com/2015/06/29/how-to-passing-a-new-variable-out-of-flow/
http://leehildebrand.name/?p=223

https://andyinthecloud.com/2014/10/26/calling-flow-from-apex/

https://stackoverflow.com/questions/10032799/how-do-i-get-a-variable-back-from-a-flow
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);
    myFlow.start();
    
}
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;
    OpportunityProductFlowController.returnOpportunityId(oli);

}