Advanced Salesforce Flows: Sub-Flows, Apex & Error Handling

February 26, 2025
191 Views
Advanced Salesforce Flows: Sub-Flows, Apex & Error Handling

Salesforce Flows is one of the most transformative automation tools for businesses. What’s more? It’s a no-code tool. You can build custom workflows using just clicks. Want to elevate your automation game further? Salesforce Flows offers advanced features that can help you build customized, scalable flows. These features include Sub-Flows, Apex Variables, and Error Handling. Effectively using these features can help you maximize output, reduce costs, and save your team’s time.

What are Sub-Flows?

Want to break your Flow into smaller and easier-to-manage components? Sub-flows allow you to do that. They can be called from a parent Flow. They are usually created with functionalities that are needed in multiple Flows. This means you won’t have to build the same functionality for various other places.
Benefits of Sub-Flows
  • They are reusable: As mentioned above, once you build a sub-flow, you can use it in multiple places. This saves time and effort. 
  • They are easier to manage: Using a Sub-Flow means you can break complex flow into smaller components. This makes it easier to maintain. 
  • They increase efficiency: You don’t have to repeatedly build the same functionality multiple times for various places. You can simply use the Sub-Flow.

Real-Life Use Cases of Sub-Flows

Sub-Flows can be used in many different scenarios. Some of the use cases are:- 
  • Calculate Project Due Dates

    You can create a Sub-Flow for setting the starting dates of projects and setting their due dates. For example, one of the conditions can be that the due date has to be on a working day, not a holiday.

  • Onboarding New Joiners

    A Sub-Flow can make the onboarding process easier by providing training access to new employees, storing their information, and putting them on the payroll.

  • Lead Tracking

    You can build a Sub-Flow for qualifying leads, getting their information, updating sales representatives, and much more.

  • Reusable Email Notifications

    Do you want to create email notifications for important events such as project initiation, case completion, etc? Create a Sub-Flow that will accept variables such as email ID, subject line, and message. This Sub-Flow can be invoked through the parent flow, whenever personalized email notifications are required.

  • Archiving Records

    Want to archive old records with minimal effort? Create a Sub-Flow that will select and archive records. You can also delete certain records if they are too old and irrelevant. The Parent Flow can call upon these Sub-Flows at different periods of time or during audits.

Also Read

Don’t forget to checkout: Apex Design Patterns: Best Practices for Scalable Code.

What are Apex Variables?

This is another advanced feature in Flows. Using Apex Variables, you can access and manipulate data using custom Apex classes. Why do they matter? By leveraging Apex Variables, you can elevate the power of Flows to perform complex data manipulations and calculations.
Benefits of using Apex Variables
  • Handling Complex Logic: It can be used in scenarios where complex logic is required. These are the scenarios where declarative tools are not of much help. 
  • Batch Processing: You can create batches of records and process them in groups rather than singularly. 
  • Integration with External Systems: Want to extract data in a third-party application or system? You can do this easily in real time using Apex Variables.

How to set up Apex Variables?

Benefits of using Apex Variables
  • Step 1: Go to the Developer Console.
  • Step 2: Create a new Apex Class
  • Step 3: Put in the variables you want in your Flow. You can do this by adding fields annotated with @InvocableVariable
  • Step 4: State which kind of data you want in each variable. It can be integer, string, custom object, or Date)
    For Example:
    
    public class ProductDetails {
    
    	@InvocableVariable(required=true) public String productName;
    	
    	@InvocableVariable(required=true) public Decimal price;
    	
    	@InvocableVariable public String description;
    }
    							
  • Step 5: Now, you need to define the method which can be called by the Flow. Use @InvocableMethod to do this.
  • Step 6: It’s time to connect the Apex Action to your Flow. Simply drag the Apex Action on the canvas. Choose the Apex class you created.

An Example of using Apex Variables

If you want to perform mass record updates and have to use complicated logic, you can do it easily through Apex Variable. You can move IDs from a collection variable to the Apex Class. After that, you can run the mass update logic.

Real-Life Use Cases of Apex Variables

  • Managing Inventory

    Using Apex Variables, you can update the inventory whenever a new order is created. 

  • Calculating Commission

    Want to easily calculate sales commission? Do this using Apex Variables where you use the total amount and commission rate. 

  • Give Scores to Leads

    Want to see which leads to prioritize? Use the lead scoring method where you can assign scores to leads based on factors such as their origin, level of engagement, industry, etc.

What is Error-Handling?

While Salesforce Flow is a powerful tool, there are chances of errors with every technology. In the case of Flows, errors can occur due to a variety of different factors. It could be due to incorrect data formats, permission problems like inadequate access, missing information in required fields, etc. Want your Flow to function seamlessly? Master the advanced feature of error handling for a hassle-free experience. Error handling is the process of recognizing issues and resolving them so that the Flow runs smoothly.

Benefits of Error Handling

Needless to say, there are multiple benefits if error handling is done properly. Some of them are:- 
  • Users get a better experience.
  • It’s easier to maintain.
  • It saves time.
  • It provides you with a more stable system without abrupt failures.

Techniques of Error Handling

  1. Creating Fault Paths

    This is the most commonly used way of error handling. 

    When you define a fault path, you can clearly define which actions should be taken when an error occurs. It could be recoding the error, showing an error message, or shifting the user to another screen. 

    You can use fault connectors on elements to do this. These elements can be Record, Create, Update, and Delete to define alternative paths whenever there is an error. 

    For example, you can have it send a message to the admin in case of an error.

  2. Use Try-Catch logic in Apex

    Here, you can use try-catch blocks to manage exceptions in custom Apex code. 

    The method uses a Try Block which has code that may toss exceptions. 

    Then, there is a Catch Block which executes when there is an exception in the try block. 

    For example, you can create an integration process. This will create and delete a contact with the particular LastName and send an error message if the LastName is blank.

Real-Life Use Cases of Error-Handling

  • Permission-related Errors

    If a record is updated but the user lacks permission to edit it, you can notify the user that they lack access. You can also inform the admin of the issue. 

  • Catch missing data

    When creating automated tasks, you can catch which data is missing. You can also notify the owner that certain data is missing. 

  • Insertion of Duplicate Records

    With adequate error handling, you can avoid duplicity in records. This can be done by searching existing records before creating a new one.

Advanced Tips & Best Practices

  • Do not leave error handling for the end. Want your Flow to run smoothly? Make sure that you identify and resolve errors at different stages rather than only when the process is complete.
  • Get to the root of the error. You can do this by using debug logs and execution details of the Flow. It helps in the quick identification of issues. 
  • Make sure you keep a record of errors. By doing this, you will gradually be able to see patterns and possible problems that you need to resolve.

Conclusion

If you want to maximize output from Salesforce Flows, master these advanced elements. This will not only save you time, energy, and resources but also give you an edge. Leveraging this powerful automation tool requires continual upskilling and staying informed about new features. Curious to know more? Check out the 10 Best Practices for Designing Efficient and Scalable Salesforce Flows.

FAQs

Use the “Debug” function in Flow Builder. Using this, you will be able to see the values of variables at every step of the flow. This also involves Apex Variables. You can run the Flow in debug mode and analyze the debug logs. Want to analyze variable values even further? You can add customized debug messages within your Apex code.

Written by

user

Mohit Bansal

Salesforce Technical Architect | Lead | Salesforce Lightning & Integrations Expert | Pardot | 5X Salesforce Certified | App Publisher | Blogger

Contributor of the month
contributor
Gopinath G

Passionate about the intersection of cutting-edge technologies

Get the latest tips, news, updates, advice, inspiration, and more….

...
Boost Your Brand's Visibility

Want to promote your products/services in front of more customers?

...