Invocable Methods in Flows – Salesforce Automatization Tools – Salesforce Certified Platform Developer I Study Guide

3.6  Invocable Methods in Flows

Invocable methods in Salesforce allow developers to expose Apex methods as custom actions in the Flow Builder. These custom actions can be used in flows to perform complex logic that cannot be achieved with declarative tools alone. Invocable methods can receive input parameters from flows, perform some processing, and return output values. The input and output parameters can be of primitive data types, sObjects, or collections of these data types.

To use an invocable method in a flow, you must first create the method in Apex with the @InvocableMethod annotation.

The input and output parameters must be declared as public or global, and must be of the supported data types. Further, the parameters need to be accessible, they should specifically be annotated with @InvocableVariable. Once the method is created, it can be selected as an Apex action in the Flow Builder.

In a flow, the Apex action can be added to the canvas and configured with input parameters. These input parameters can be derived from variables or values obtained from other elements in the flow, such as record lookups or user inputs. When the flow runs, the Apex action is executed with the specified input parameters, and returns output values that can be used in subsequent flow elements.

Considerations when Using Invocable Methods in Flows

When using invocable methods in flows, there are some considerations to keep in mind. First, invocable methods should be designed to be bulkified, as flows can process multiple records at once. This means that the methods should be able to handle collections of input and output parameters, and not just a single record. Additionally, the input and output parameters should be of supported data types, as flows are bulkified and can run multiple interviews in one transaction.

It is also important to note that invocable methods run in system context by default, which means that the sharing rules and record-level access are not enforced. This can be a security risk if the method is used to modify or access sensitive data. It is recommended to use invocable methods only for non-sensitive operations, or to add appropriate security checks in the method implementation.

Overall, invocable methods provide a powerful way to extend the capabilities of Salesforce flows and perform complex logic using Apex. By designing the methods to be bulkified and using supported data types, developers can create reusable and efficient solutions that can be easily maintained.

Let’s add an action to our created flow, where @InvocableMethod is used to encapsulate custom logic that updates opportunity fields. This could be useful if the logic involves complex calculations, updates to multiple fields, or other operations that are better handled in Apex code. The flow triggers this logic after creating the task when the opportunity reaches the Negotiation/Review stage. We will set Close Date to three days from the current date for opportunities that have reached the Negotiation/ Review stage. This ensures that the Close Date is updated only for opportunities in the specified stage.

Step 1: We need to create new Apex class “OpportunityUpdater” including @InvocableMethod as follows. Use the Developer Console menu ➤ File ➤ New ➤ Apex Class, see more information in the next chapter.

public class OpportunityUpdater {

public class OpportunityUpdateInput {

@InvocableVariable(label=’Opportunity Id’ required=true)

public Id opportunityId;

}

@InvocableMethod(label=’Update Opportunity Fields’ description=’Updates opportunity fields based on custom logic’) public static void updateOpportunityFields (List<OpportunityUpdateInput> inputList) {

  • Implement your custom logic here (if needed)
  • You can access the opportunityId from each input and
  • perform the necessary updates

List<Opportunity> opportunitiesToUpdate = new List<Opportunity>();

// Retrieve opportunities with the ‘Negotiation/Review’ stage

List<Opportunity> negotiationReviewOpportunities = [SELECT Id, StageName, CloseDate FROM Opportunity WHERE StageName = ‘Negotiation/Review’];

for (Opportunity oppToUpdate : negotiationReviewOpportunities) {

  • Set Close Date to three days from today oppToUpdate.CloseDate = Date.today().addDays(3); opportunitiesToUpdate.add(oppToUpdate);

}

// Update the opportunities

if (!opportunitiesToUpdate.isEmpty()) { update opportunitiesToUpdate;

}

}

}

The provided Apex code represents a class called “OpportunityUpdater” with a nested class called “OpportunityUpdateInput” and a static method called “updateOpportunityFields.” The code is designed to update the CloseDate field of opportunities with the StageName set to “Negotiation/Review” to three days from the current date.

The “OpportunityUpdateInput” class has a single member variable, “opportunityId,” of type Id. This variable is marked with the “@InvocableVariable” annotation, indicating that it is an input variable for an invocable method.

The “updateOpportunityFields” method takes a list of “OpportunityUpdateInput” objects as input. Inside the method, a query is used to retrieve a list of opportunities with the StageName set to “Negotiation/Review.” The CloseDate field of each opportunity is then updated to three days from the current date. The updated opportunities are added to a list called “opportunitiesToUpdate.” Finally, if the “opportunitiesToUpdate” list is not empty, the opportunities are updated using the “update” keyword.

Step 2: Now open new created “Opportunity review” Flow and click + to add the new action to our created flow after Opportunity_ID element on the canvas.

Step 3: Choose Type on the left menu.

Step 4: Choose Apex Action ➤ Update Opportunity Fields.

Step 5: Put the name New update in the Label field, and API Name will be automatically populated.

•\ Opportunity Id field ➤ {!Opportunity_ID.Id}

Step 6: Your Flow is updated as below.

•\ Save your updated Flow and ensure that it is activated.

•\ Check the updated Flow by building a new Opportunity and

changing the Stage to Negotiation/Review.

•\ See the updated Close Date of the related Opportunity.