After the release of Summer ’14, it is possible to call the flows from apex classes using Flow. Interview Class and its start method to start the flow. You can use the flow in the Visualforce page also using the flow: interview component, but in this case, we need to use UI and User Interaction to drive it.

The Flows in Salesforce can be triggered. Ready flows mean a flow that can be launched without user interaction. In this case, the flows are called using Workflow actions or Process Builder. Here, we are just making the Flow and calling it through apex code and a scheduled class, which calls the controller method to execute the flow.

Prerequisites

1. Salesforce Setup:

Ensure you have access to Salesforce with the necessary permissions to create and modify Flows and Apex classes.

2. Basic Knowledge:

Familiarity with Salesforce’s Flow Builder and basic Apex coding is required to follow this guide effectively.

Call Flows From Apex Example

Flow Example

About Call Flows in Salesforce

In this flow, we are getting the record from the Lead to make a decision. If all the required fields for a hot lead are not null, then we are sending an email to the owner.

When the lead is saved, it goes to the record variable of flow that is “new Lead,” and then we save it to Lead fields and then proceed to the following process.

edit get records
hire salesforce developers

Also Read: How To Embed Lightning Component In Flow

Step-by-Step Guide with Code Examples

Step 1: Understanding Flows and Apex

Salesforce Flows automate processes visually, while Apex is used for complex logic. Integrating them allows for more dynamic and powerful automation in Salesforce.

Step 2: Creating a Flow

  • Navigate to Flow Builder: In Salesforce, go to Setup > Process Automation > Flows.
  • Create a New Flow: Choose a flow type suitable for your use case, like Record-Triggered or Auto-Launched.
  • Design the Flow: Add elements such as decisions, assignments, and actions. For instance, create a flow that sends an email when a new lead is added.

Step 3: Preparing Apex Code

  • Open Developer Console: In Salesforce, navigate to the Developer Console.
  • Create a New Apex Class: Name it, for example, FlowController.
  • Import Namespaces: Use System.Flow statements to import necessary namespaces.
public class FlowController {
    // Class content goes here
}

4. Calling the Flow from Apex

  • Instantiate the Flow: In your Apex class, instantiate the Flow using System.Flow.Interview.
public class FlowController {
    public void callFlow() {
        // Set up Lead record as an example
        Lead l = new Lead(FirstName = 'Sapna', LastName = 'Chandani', Email = '[email protected]', Phone = '8522256355', MobilePhone = '876545465', Company = 'ExampleCompany');
        insert l;
        
        // Prepare the Flow variables
        Map<String, Object> params = new Map<String, Object>();
        params.put('newLead', l);
        
        // Instantiate and start the Flow
        Flow.Interview.Your_Flow_Name yourFlow = new Flow.Interview.Your_Flow_Name(params);
        yourFlow.start();
    }
}
  • Ensure to replace Your_Flow_Name with the actual name of your Flow.

5. Testing and Debugging

  • Test the Apex Code: Create Apex Test Classes to ensure the functionality works as expected.
  • Debug: Utilize System Debug logs and Flow debug tools to troubleshoot issues.

6. Use Case Example

  • Example Scenario: An Apex trigger is set to call a Flow when a lead’s account status changes, updating contact records accordingly.

Additional Code Snippets

  • Flow Controller Class: The FlowController class is already outlined above.
  • Controller Using the Visualforce Page: Create a Visualforce page to trigger the flow.
<apex:page controller="FlowController">
    <apex:form>
        <apex:commandButton action="{!callFlow}" value="Start" reRender="text"/>
    </apex:form>
</apex:page>
  • Schedulable Class: For automating the flow execution at scheduled intervals.
global class scheduledFlow implements Schedulable {
    global void execute(SchedulableContext sc) {
        FlowController fc = new FlowController();
        fc.callFlow();
    }
}

If you are searching for professional salesforce consultants, contact us.

Also Read: How To Create A Flow In Salesforce

Best Practices To Follow While Calling Flows From Apex In Salesforce

  • Ensure robust error handling in both Apex and Flows.
  • Maintain clear documentation and comments for better readability and maintenance.
  • Optimize Flow and Apex code for performance.

Troubleshooting Common Issues

  • Flow Not Triggering: Check if the Flow is activated and if the input variables in Apex match those in the Flow.
  • Apex Errors: Validate data types and ensure error handling is in place for null values and exceptions.

Conclusion

Calling flows from Apex in Salesforce is a powerful technique offering developers extensive flexibility and control over their applications. Integrating flows into Apex code allows a Salesforce development company to streamline complex processes, automate tasks, and enhance the overall user experience.

This integration empowers developers to create more dynamic, efficient, customized solutions within the Salesforce platform. Any Salesforce development company needs to master this skill to leverage the full potential of Salesforce, ensuring its clients receive the most advanced and efficient solutions possible. Remember, the key to success in Salesforce lies in continually evolving with its versatile features.

Avatar photo
Author

With a decade of experience in eCommerce technologies and CRM solutions, Virendra has been assisting businesses across the globe to harness the capabilities of information technology by developing, maintaining, and improving clients’ IT infrastructure and applications. A leader in his own rights his teammates see him as an avid researcher and a tech evangelist. To know how the team Virendra can assist your business to adopt modern technologies to simplify business processes and enhance productivity. Let’s Talk.

whatsapp