Getting Started with Salesforce Flow – Part 44 (Auto Convert Marketing Qualified Leads)

Getting Started with Salesforce Flow – Part 44 (Auto Convert Marketing Qualified Leads)

Last Updated on January 4, 2021 by Rakesh Gupta

Big Idea or Enduring Question:

How do you set automation to auto-convert marketing qualified leads? 

When there is a chance of further negotiations with a lead, it can be converted into an account, contact, optionally an opportunity. In short, once the lead status has reached a certain stage, it can be qualified as a potential. On conversion, all the lead details are transferred in creating an account, contact, and optionally an opportunity. The lead conversion process is a manual process, if you want to automate it then you have to use Apex code. This article will help you to understand how to automate the lead conversion process using the Lightning Flow. 


After reading this blog post, the reader will be able to:

  • Use After-save Record-Triggered Flow to auto-convert marketing qualified leads 
  • Understand @InvocableMethod Annotation
  • Use Schedule-Triggered Flow to call an Apex method

Business Use Case

Pamela Kline is working as a System administrator in Universal Container. She has received a requirement from the management to auto-convert marketing qualified  (i.e. leads with Rating = Hot) leads and create an account and contact record.

Automation Champion Approach (I-do):

While this can be solved using various automation tools like Apex or Process Builder and Apex, we will use After-save Record-Triggered Flow and call an Apex method. To call an Apex method, add the Call Action element to your flow and select an Apex class with a @InvocableMethod Annotation. It means they allow us to extend the Lightning Flow by writing Apex code that meets certain criteria and then invoking the Apex from our Flows. If the class contains one or more invocable variables, manually enter values or reference field values from a related record. @InvocableMethod Annotation supports bulk operations. Lets begin the solution for the business use case.
In this article, we will use the call LeadConvert database class from Apex code. Make sure to review it. 
Before discussing it, let me show you a diagram of a Process Flow at a high level. Please spend a few minutes to go through the following Flow diagram and understand it.

Let’s begin building this automation process.

Guided Practice (We-do):

There are 2 steps to solve Pamela’s business requirement using Lightning Flow. We must: 

  1. Create an Apex class and test class
  2. Lightning Flow Steps:
    1. Define flow properties for record-triggered flow
    2. Adding an action to Call an Apex Class

Step 1: Create an Apex class and Test class   

  1. Click Setup.
  2. In the Quick Find box, type Apex Classes.
  3. Clicks on the New button.
  4. Copy code from GitHub and paste it into your Apex Class. 
  5. Click Save.

Repeat the above steps and click the Test class. You can get the code from my GitHub repo. 

Step 2.1: Lightning Flow – Define Flow Properties 

  1. Click Setup.
  2. In the Quick Find box, type Flows.
  3. Select Flows then click on the New Flows.
  4. Select the Record-Triggered Flow option and click on Next and configure the flow as follows: 
    1. How do you want to start building: Freeform
    2. Trigger the Flow When: A record is created or Updated
    3. Run Flow: After the record is saved
    4. Object: Lead
  5. Select All Conditions Are Met (AND)
  6. Set Conditions
    1. Row 1
      1. Field: Lead | Rating
      2. Operator: Equals 
      3. Value: Hot
  7. Choose the Option to Only when a record is updated to meet the condition requirements.
  8. Click Done.

Step 2.2: Lightning Flow – Call an Apex Class

The next step is to call the AutoConvertLeads class from flow so that when flow fires it auto-convert the leads.

  1. Under Toolbox, select Element
  2. Drag-and-drop Action element onto the Flow designer. 
  3. In the Action box, type AutoConvertLeads.
  4. Clicks on the AutoConvertLeads apex class
  5. Enter a name in the Label field; the API Name will auto-populate.
  6. Set Input Values: 
    1. Row 1:
      1. LeadIs{!$Record.Id}
  7. Click Done.

In the end, Steven’s Flow will look like the following screenshot: Once everything looks good, perform the below steps: 

  1. Click Save.
  2. Enter Flow Label the API Name will auto-populate.
  3. Click Show Advanced.
  4. Type: Record-Triggered Flow
  5. API Version for Running the Flow: 50
  6. Interview Label: Auto Convert Leads {!$Flow.CurrentDateTime}
  7. Click Save

Almost there! Once everything looks good, click the Activate button.

Proof of Concept

Now onwards, if a business user updates the Lead Rating to Hot, Lightning Flow will automatically convert such leads and create an account as well as a contact. 

  1. Currently, the lead Gaurav Pradhan is Open and the Rating is Warm as shown in the following screenshot:
  2. Now we update the Rating to Hot and see the flow magic. 
  3. Flow auto converts the Lead and create an Account and Contact or it.

Note: –  I will suggest you implement this first in your developer org, test it and then move it to production. The same approach will work for the case assignment rule. Download Apex and Test class from GitHub. 

Process Builder Approach

Formative Assessment:

I want to hear from you!  

What is one thing you learned from this post? How do you envision applying this new knowledge in the real world? 

Let me know by Tweeting me at @automationchamp, or find me on LinkedIn.

Have feedback, suggestions for posts, or need more information about Salesforce online training offered by me? Say hello, and leave a message!

117 thoughts on “Getting Started with Salesforce Flow – Part 44 (Auto Convert Marketing Qualified Leads)

  1. I have the Class working, thank you for the code! However, I cannot get the test class to work. I keep getting this error when I run the tests:
    Methods defined as TestMethod do not support Web service callouts
    As a result I cannot push the Class and ClassTest to my Production org. I have been fighting with it for two days now. Any thoughts?

  2. I continue to get the same error when running the Apex Test Class –
    Methods defined as TestMethod do not support Web service callouts Stack Trace: null
    I cannot figure out how to fix it !

  3. How can we add the outputs to this code. I am trying to use this in a flow that updates the opportunity record type as well. I need the outputs from the method to do it. Any suggestions?

    1. Were you getting the I have the “Methods defined as TestMethod do not support Web service callouts” error? That is what I am getting, and still cannot get it to work,

        1. Holy crap I feel stupid! I read through the debug log, and found an old managed package that was kicking it out. Uninstalled it, and now we are golden! Thank you very much for your help, and I love this new functionality!!

  4. This was extremely helpful! I did make one minor edit to the test class due to an error. On line 4:
    User userToCreate = [Select id from user where’System Administrator’ Limit 1];’

    I changed it to:
    User userToCreate = [Select id from user where’System Administrator’ AND IsActive=TRUE Limit 1];’

    Without the “IsActive” filter, it may try to assign the Lead to an inactive user, causing an error.

    Thanks for this!!

  5. Hey! Question, I need help on your previous video “Auto Convert Qualified Lead using Lightning Flows”.

    How did you setup that apex class? I can’t find where or how to do it.

    I go setup->Apex classes but there is no ‘New’ button. Just ‘Developer Console’, ‘Run All Tests’, and and ‘Schedule Apex’.

    I’ve tried going through Developer Console->File->New->Apex Class but after I ‘OK’ after giving it a name it says “ENTITY_IS_LOCKED Can not create Apex Class on an active organization.” . How do I get passed that?

  6. Even before I asked you I thought about it and myself did not like the fact that I have to create few processes to solve one problem.

  7. Hi Rakesh,

    Instead of involving Apex class for the above given problem, can we solve it using Record Triggered flows?.

    Here is what I was thinking..

    On the lead Object I can create Lead Converted field of type checkbox.

    1Have a before save update trigger on the lead object which will update checkbox field on the lead object as soon as Rating is Hot

    2)Then After save trigger on the lead object which will check whether the checkbox field on the Lead object has been checked if it is then create an Account record, mapping its field to the field on the lead object.

    3)Have another trigger After Save Update Trigger on the Lead Record which will be used to create contact record .

    Any suggestions?


    1. Good thought process. Nice work.

      Do you think having three Flows to solve one business requirement is an optimal solution when you can achieve it using one Flow with few lines of code?

  8. Hi Rakesh, I tried the test class provided in GitHub and it does not work. Can you help with Test Class for the code in this page?

  9. Hi, thank you for the great Idea. This is cool. At it is working already quiet well.

    May I ask one question: How can I adjust the process so that the auto-convert only fires for existing accounts? I don’t want that new accounts are created automatically.

    Here is the current apex:

    Public class AutoConvertLeadsAccount
    public static void LeadAssign(List LeadList)
    List LeadAcc = new List();
    for(Lead L : LeadList){
    list AccList = [SELECT ID, Name FROM Account WHERE Name IN :LeadAcc];
    LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1];
    List MassLeadconvert = new List();
    for(Lead L : LeadList){
    Database.LeadConvert Leadconvert = new Database.LeadConvert();
    for(Account A : AccList){
    if(L.Company == A.Name){
    Leadconvert.setDoNotCreateOpportunity(TRUE);//Remove this line if you want to create an opportunity from Lead Conversion

    if (!MassLeadconvert.isEmpty()) {
    List lcr = Database.convertLead(MassLeadconvert);

    thank you.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.