Passing Wrapper from Apex to Flow in Salesforce

July 8, 2024


 

In Salesforce development, integrating Apex with Flows allows for powerful automation and customization. One common scenario is passing complex data structures, such as wrapper classes, from Apex controllers to Flows. Let's explore how to achieve this seamlessly.

After completing this blog, you’ll be able to:

  1. Create an Apex Class
  2. Create a Flow
  3. Create an Apex Class for Handling Flow Output
  4. Call the Flow from Anonymous Apex
Step 1: Creating the Apex Class

First, let's create a simple wrapper class in Apex that we'll use to pass data to our Flow.

FlowInvoker.apxc

public with sharing class FlowInvoker {
   @AuraEnabled
   public String firstName;
}

This class defines a single property firstName, which we will populate and pass to a Flow instance.

Step 2: Creating the Flow

Next, we'll create a Flow in Salesforce that will receive an instance of our FlowInvoker class as an input variable.

  1. Create a Flow (PassWrapperInFlow):

    • Go to setup, search flows and click on flows.
    • Click New Flow, select Autolaunched flow then click on create.
  2. Create an Input Variable:

    • Data type: Apex-Defined
    • Apex class: FlowInvoker
    • Name: inputVariable
    • Select Available for input
  3.  

     

  4. Create an Output Variable:

    • Data type: Apex-Defined
    • Apex class: FlowInvoker
    • Name: outputVariable
    • Select Available for output
  5.  

     

  6. Add an Assignments element:

    • Configure the assignments as needed to process the input variable.
     

     

  7. Add an Action element:

    • Add any actions that you want to perform in the Flow.
     

     

  8. Save and Activate the Flow:

    • After designing the Flow, save it and activate it so it can be invoked from Apex.
     

     

Step 3: Apex Class for Handling Flow Output

Create an additional Apex class with an invocable method to handle the output from the Flow.

FlowHandler.apxc

public class FlowHandler {
   public class MyWrapper {
      @InvocableVariable
      public String firstName;
   }

   @InvocableMethod
   public static void handleFlowOutput(List<MyWrapper> wrappers) {
      for (MyWrapper wrapper : wrappers) {
         System.debug('firstName: ' + wrapper.firstName);
      }
   }
}

Step 4: Calling the Flow from Anonymous Apex

Note: Ensure that the Flow and Apex classes are saved and activated properly before testing.

Now, let's invoke the Flow we created from an Anonymous Apex script. This script will instantiate our FlowInvoker class, populate its firstName property, and pass it as an input to the Flow.

FlowInvoker wrapper = new FlowInvoker();
wrapper.firstName = 'TestfirstName';
Map<String, Object> inputVariables = new Map<String, Object>();
inputVariables.put('inputVariable', wrapper);
Flow.Interview myFlow = Flow.Interview.createInterview('PassWrapperInFlow', inputVariables);
myFlow.start();
FlowInvoker outputValue = (FlowInvoker)myFlow.getVariableValue('outputVariable');
System.assert(false, 'Output Value: ' + outputValue);

This script will print the outputVariable from the Flow, ensuring that the data has been passed and processed correctly.

Conclusion

By following these steps, you can pass complex data structures from Apex to Flows in Salesforce, enhancing your automation capabilities and making your processes more dynamic and robust.

  • Connect with us

  • sales@phenoble.com

  • hr@phenoble.com

  • +91 7850952954


© 2025 Phenoble Software Private Limited