How to call Apex Class From Salesforce Flow

How to call Apex Class From Salesforce Flow

This blog post will show how to call an Apex class from a Flow using an Invocable Method. We’ll create a Flow that updates the Account’s Site field with the Website value entered in an Opportunity.

Invocable Method

To call Apex code from a Flow, you need to create an Invocable Method within your Apex class. This method is annotated with @InvocableMethod, which makes it accessible to the Flow.

The label attribute in the annotation specifies the display name of the Apex Action. Each class can contain only one Invocable Method and accept a single parameter.

Method Requirements

  • Class Visibility: The Apex class must be defined as public or global.
  • Method Visibility: The Invocable Method must be static and public or global.
  • Class Type: The class must be an outer class.
  • Invocable Method Limit: There can be only one Invocable Method per class.

Let’s call Apex Class from Flows

Use Case – 

When an opportunity is created from the account page, the account’s site field in the account details will be updated with the website value entered in the Opportunity.

1. Create an Apex class with @InvocableMethod

call Apex Class from Flows

2. Configure the Class in the Flow

a. Create screen flow

  • Go to Setup > Flows > New Flow.
  • Choose Screen Flow as the template.
  • Drag the Screen element into the flow builder and Configure the Screen properties by API name and Label.
  • Drag the following input from the component pallet into the Screen canvas –
    Text —-> Opportunity Name
    Date —-> Close Date
    PickList —–> StageName
    Text —–> Website

b. Create two Variables 

  • recordId – This variable is used to store the Account Record ID. When the flow is triggered from an Account record (for example, when a user clicks the flow button on the Account page), the current Account’s unique ID is passed into this variable.

call Apex Class from Flows

  • OpportunityId – This variable is used to store the Opportunity ID after the Opportunity record is created within the flow. It is initially empty and will be populated with the ID of the newly created Opportunity once the flow executes the “Create Record” action.

c. Create Record elements

  • Drag the ‘Create Record’ element onto the canvas and Configure the API name and Label.
  • Select “Opportunity” as the object to create.
  • Set the Field Values for the Opportunity
  1. Field  – AccountId and  Value – {!recordId}
  2. Field – CloseDate and Value – {!Close_Date}
  3. Field – StageName and Value – {!StageName}
  4. Field – Name and Value – {!Opportunity_Name}

 

call Apex Class from Flows

  • Check the box for “Manually assign variables”.
  • Provide Variable to Store Opportunity ID.
  • Click on Done.

d.  Action element

  • Drag and drop an Action element onto the canvas.
  • In the Filter by dropdown, select Type.

  • Click on Apex Action.
  • Select the Apex class you created earlier that contains the Invocable Method.

call Apex Class from Flows

  • Provide a Label and API Name for the action.
  • Provide Set Input Values for the Selected Action – {!OpportunityId}.
  • Expand the drop-down – Show Advanced Option.
  • Check the box for Manually assigned variables.
  • Now, Create and assign a new variable to store the output values from the action.

call Apex Class from Flows

  • Click on Done.

e. Screen Flow

  • Go to Setup > Flows > New Flow.
  • Choose Screen Flow as the template.
  • Drag the Screen element into the flow builder and Configure the Screen properties by API name and Label.
  • Drag the ”Display Text”  input from the component palette into the Screen canvas –
  • Use the {!outputValues} variable to display the output, including details from the Opportunity record.

call Apex Class from Flows

f. Configure the flow on the Account’s page

  • On any Account record page, click the gear icon.
  • Select Edit Page to open the Lightning App Builder.
  • In the builder, drag and drop the Flow component onto the page.
    call Apex Class from Flows

call Apex Class from Flows

  • Check on the checkbox of ‘Pass record ID in this Variable.

g. The Flow Will Appear as Follows

h. See, how the flow will work on the UI

Are you preparing for the Salesforce AI Certifications? Check out the Salesforce certification practice set here

FAQs

1.  How can we call flows from the Apex class?

To initiate a Flow from an Apex class, you can create an instance of the FlowInterview object using the FlowInterview.flowName syntax. This object subsequently employs the start() method to trigger the execution of your Flow.

2. How can we schedule the Batch job from flows?

We can schedule a Batch job from flows by selecting Batch Job in the Category Section from the Action element.

Conclusion

In this blog post, we explained how to use an Invocable Method to call an Apex class from a Flow. Following the instructions, you can create a Flow that updates the Website value entered in an Opportunity to the Account’s Site field. We also covered important topics like the requirements for Invocable Methods, how to set up flows and how to use Apex actions in flow.

Get a complete Roadmap To Learn Salesforce Admin And Development

Share Now

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *