Given a scenario, use declarative functionality and Apex together to automate business logic.

After studying this topic, you should be able to:

  • Determine how to use declarative and programmatic customization to meet a requirement
  • Describe how to invoke Apex from a declarative tool and invoke flow from Apex
  • Identify scenarios where Apex and a declarative functionality is used in a solution

Table of Contents

  • Declarative Customization
  • Invoke Apex in Process Builder
  • Invoke Apex in Flow Builder
  • Invoke a Flow from Apex
  • Scenarios and Solutions

Introduction

  • Process Builder and Flow Builder allow invoking Apex code from processes/flows
  • @InvocableMethod makes Apex methods available to a flow or process
  • Its also possible to launch a flow from Apex
  • Only use programmatic capabilities as a last resort, when declarative features alone do not meet the business requirements
    • Declarative customizations can be used for simple to complex requirements
    • Programmatic customizations can be used for more complex requirements, like performing sophisticated validation over multiple objects

Declarative Customization

  • Ongoing feature upgrades to Flow Builder mean that Flows are now able to achieve some requirements that in the past had only been accomplishable with Apex
  • Code is required if the logic for a requirement is very complex
  • Solutions for Complex scenarios, Simple scenarios…
    • Automatically create a record: Flow/Process calls a Flow, or Apex trigger, Flow/Process
    • Automatically delete a record: Apex trigger, Flow
    • Automatically update a record: Flow/Process calls a Flow, or Apex trigger, Flow/Process/Workflow rule
    • Create a record from an email: Email handler (Apex), configuration of email-to-case
    • Generate a report: Visualforce with custom controller, custom lightning component, Report Builder
    • Schedule a recurring job: Apex Scheduler, Flow
    • Share a record: Flow Builder/Apex Managed Sharing, Configuration of sharing rules, teams
    • Validate a record: Apex Trigger, Configuration of Validation Rules
    • Call a web server: Custom Web Services (Apex), Configuration of outbound message
    • Build a wizard to guide users: Visualforce w/ custom controller, or Custom Lightning Component, Flow

Invoke Apex in Process Builder

  • Apex actions can be created and added to the process to invoke Apex code
  • Apex actions are defined with an Action name and Apex class for adding Apex code functionality to a process
    • Method Annotation: @InvocableMethod annotation is used in Apex classes to identify methods that can be run as invocable actions - must be static and either public or global
    • Only a single method can have an InvocableMethod annotation, and other annotations cannot be used with it
    • Invocable Variable: an invocable method can have at most one input parameter, and @InvocableVariable annotation can be used to identify class variables used as input or output parameters
    • Values: if the class includes an invocable variable, values for sObject or primitive type list variables can be entered manually or field values from a related record can be used
    • Attributes: label and description are supported by invocable methods and variables
    • Required Variable: invocable variables support the required modifier, which specifies whether the variable is required and adds a prompt in the UI

public class OpportunityAction{
    @InvocableMethod(label='Update Opportunity Shipment Status' 
                     description='Updates Shipment Status when Opportunity is Closed Won.')
    public static void UpdateOpportunityShipmentStatus(List<Id> OpportunityIds)
    {
        List<Opportunity> opportunities = [SELECT Id, Status__c 
                                             FROM Opportunity 
                                            WHERE Opportunity.Id in :opportunityIds];
        for(Opportunity opp: opportunities){
            opp.Status__c = 'New';
        }

        update opportunities;
    }
}

Invoke Apex in Flow Builder

  • Possible to call Apex code from one of two flow elements: Apex Plug-in and Call Apex
    • Invoking Apex from Flow: use Process.Plugin interface or @InvocableMethod annotation
    • Values can be passed into Apex-defined variables when a flow is launched from a Subflow element, a Visualforce page or as an action

global class LookUpShipment {
    @InvocableMethod(label='Get Shipment ID'
                     description='Get Shipment ID from Shipment Name'
                     category='Logistics')
    public status List<String> getShipmentIds(List<String> names) {

        List<Id> shipmentIds = new List<Id>();
        List<Shipment__c> shipments = [SELECT Id
                                         FROM Shipment__c
                                        WHERE Name IN :names];
        for (Shipment__c shipment : shipments) {
            shipmentIds.add(shipment.Id);
        }
        return shipmentIds;
    }
    return shipmentIds;
}
  • Salesforce recommends using @InvocableMethod annotation instead of Process.Plugin interface due to its more advanced capabilities over the interface.

Reference FoF Slides for Process.Plugin interface info

Invoke a Flow from Apex

  • Flows can be invoked from Apex code:
    • Flows can be called from Apex code using the Flow.Interview system class
    • start method of the Flow.Interview class can be used to launch an autolaunched flow or user provisioning flow from Apex
    • The getVariableValue method can be used to return the value of the specified flow variable
    • start method does not have a return type, but the getVariableValue method has a return type of Object
  • Some considerations apply to the use of invocable methods and invocable variables:
    • Invocable methods and variables support generic sObject and List data types
    • Support for sObject data types enables an Apex action to be reused across multiple object types
    • Invocable methods can be categorized in Flow Builder by setting the category parameter in the annotation

  • Example below shows options available for configuring an invocable Apex method:

  • When an Apex class is used as an input or output parameter for an invocable method, the class member variables that need to be available to the method should be annotated with @invocableVariable

  • A flow can be referenced in Apex by defining the flow using the Flow.Interview.flowName syntax. Example below uses this method to create a flow interview object and then invokes it

  • In this example, a flow variable called “WelcomeText” is assigned the value of “Welcome to the flow!” using the Assignment Element in Flow Builder

  • Sample code using the Flow.Interview.flowName syntax prints the following in the debug log when executed. The getVariableValue() method retrieves the flow variable

  • Flows can be started from Apex code dynamically. In the example below, an Apex method launches a specific flow based on the flow name that is passed to it as a parameter

  • Following illustrates using the dynamicFlowMethod example. It accepts a namespace, a flow name, and parameters to invoke a flow dynamically.

  • “WelcomeText” flow variable is retrieved in Apex using the getVariableValue() method and printed in the debug log as shown.

Scenarios and Solutions

Reference FoF Slides