Last Updated on February 10, 2022 by Rakesh Gupta
Big Idea or Enduring Question:
How to utilize the Flow and set up email-to-lead in salesforce?
Salesforce Flow is a powerful feature provided by Salesforce to minimize the code in your organization. By using it you can create, update, delete records everything just drags and drop. With the recent enhancements (Record-triggered Flow, Schedule-triggered Flow) Salesforce giving more power to admins to create automation with NO code.
Salesforce provides web-to-email features that allow organizations to capture leads from their website. At the same, Salesforce doesn’t offer the email-to-lead feature. There are a few apps available on AppExchange for this purpose. In this article, we will create an automation that allows users to create a lead by sending an email to a specific email address.
Objectives:
After reading this blog post, you will be able to understand:
- When should you use Before-save vs after-save record-triggered flow?
- How to setup Email-to-case in Salesforce
- Use After-save Record-Triggered Flow to create a record
- Use After-save Record-Triggered Flow to delete a record
Business Use case
Corey Eridon is working as a System Administrator at Universal Containers (UC). He has received a new requirement to implement email-to-lead for the organization so that when sales reps send an email to leadstosalesforce@gurukuloncloud.com, it will automatically create a lead in Salesforce. Make sure to assign the lead to the sales rep who sent the email.
Automation Champion Approach (I-do):
Salesforce now allows you to add time-dependent actions in the Flows, similar to the Process Builder. This is a major enhancement that will ultimately bring Flow to the next level.
There are multiple ways to solve the above business requirement. You can either use Apex trigger, Salesforce Flow, Process Builder, and more. To solve this requirement, we will use the After-save Record-Triggered Flow. Check out this article to understand why we are using after-save record-triggered flow for this scenario.
Before going ahead, take a moment here to understand how email-to-case captures the data from email and stores it in the case record.
Field Name | Case Fields |
Email Subject | Subject |
Email Body | Description |
Web Email | |
Name | Web Name |
Before discussing the solution, 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 5 steps to solve Corey’s business requirement using After-save Record-Triggered Flow. We must:
- Add a value (Lead) to the picklist case origin field on the case object
- Add a value (Email) to the picklist Lead Source origin field on the lead object
- Setup an email-to-case for case origin lead
- Setup forward from leadstosalesforce@gurukuloncloud.com to the email service address
- Salesforce Flow
- Define flow properties for record-triggered flow and check the case origin
- Add a scheduled path – 0 hrs after created date
- Add a get records element to find the sales rep user’s Id
- Add a create records element to create a lead
- Add a delete records element to delete the case
Step 1: Adding Additional Value to Case Origin Field on Case
First of all, we will add an additional value to the case origin field on the Case object.
- Click Setup.
- In the Object Manager, type Case.
- Select Fields & Relationships, then click New.
- Click on the Case Origin field.
- Navigate to the Case Origin Picklist Values and click the New button.
- Add a new value Lead, if you have any record type make sure to select the relevant record types.
- Click Save.
Step 2: Adding Additional Value to Lead Source Field on Lead
We will add an additional value to the lead source field on the Lead object.
- Click Setup.
- In the Object Manager, type Lead.
- Select Fields & Relationships, then click New.
- Click on the Lead Source field.
- Navigate to the Account/Lead Source Picklist Values and click the New button.
- Add a new value Email, if you have any record type make sure to select the relevant record types.
- Click Save.
Step 3: Setup Email-to-Case or the Case Origin Lead
Now we will add an email-to-case for the case origin lead. Refer to the trailhead module for step-by-step instructions.
Step 4: Setup Email Forward from leadstosalesforce@gurukuloncloud.com to the Email Service Address
The next step is to set up an email forward from leadstosalesforce@gurukuloncloud.com to the email service address (email-to-case). It means when someone sent an email to when a sales rep sent an email to leadstosalesforce@gurukuloncloud.com, it will forward a copy to the email service address (email-to-case) which subsequently creates a case in Salesforce.
Step 5.1: Salesforce Flow – Define Flow Properties
As we have a requirement of creating a record in (Lead) when a case is created from email with origin lead. This is why we have to use the After-save flow on Case.
- Click Setup.
- In the Quick Find box, type Flows.
- Select Flows then click on the New Flow.
- Select the Record-Triggered Flow option and click on Next and configure the flow as follows:
- How do you want to start building: Freeform
- Trigger the Flow When: A record is created
- Run Flow: After the record is saved
- Object: Case
- Select ALl Condition Are Met (AND).
- Set Conditions
- Row 1
- Field: Case | Origin
- Operator: Equals
- Value: Lead
- Row 1
- Click Done.
Step 5.2: Salesforce Flow – Add Scheduled Paths 0 Hrs After Created Date
- Under Start, select Add Scheduled Paths (Optional).
- Under SCHEDULED PATHS, click on the New Scheduled Path.
- Under Scheduled Path Details, enter the Label the API Name will auto-populate.
- Time Source: User: Created Date
- Offset Number: 0
- Offset Options: Hours After
- Click Done.
Step 5.3: Salesforce Flow – Adding a Get Record Element to Find the Sales Rep User Id
The Get Records element will be used to find the sales rep user’s Id.
- Under Toolbox, select Element.
- Drag-and-drop Get Records element onto the Flow designer.
- Enter a name in the Label field; the API Name will auto-populate.
- Select the User object from the dropdown list.
- Select All Conditions Are Met (AND).
- Set Filter Conditions
- Row 1:
- Field: Case | Email
- Operator: Equals
- Value: {!$Record.SuppliedEmail}
- Row 1:
- How Many Records to Store:
- select Only the first record
- How to Store Record Data:
- Choose the option to Automatically store all fields.
- Click Done.
Step 5.4: Salesforce Flow – Add a Create Records Element to Create a Lead
The next step is to create a new lead, for this we will use the Create Records element.
- Under Toolbox, select Elements. Drag and drop Create Records onto the canvas.
- Input the following information:
- Enter Label the API Name will auto-populate.
- How Many Records to Create: One
- How to Set the Record Fields: Use separate resources, and literal values
- Object: Lead
- Set Field Values for the Thanks
- Row 1:
- Field: LeadSouurce
- Value: Email
- Click Add Row
- Row 2:
- Field: LastName
- Value: {!$Record.Subject}
- Click Add Row
- Row 3:
- Field: Company
- Value: {!$Record.Subject}
- Click Add Row
- Row 4:
- Field: Description
- Value: {!$Record.Description}
- Click Add Row
- Row 5:
- Field: OwnerId
- Value: {!Find_Sales_Rep_User_Id.Id}
- Click Done.
Step 5.5: Salesforce Flow – Add a Delete Records Element to Delete a Case
- Drag-and-drop Delete Records element onto the Flow designer.
- Enter a name in the Label field; the API Name will auto-populate.
- For How to Find Records to Delete select Use the IDs stored in a record variable or record collection variable.
- Set Record(s) to Delete
- Record or Record Collection: {!$Record}
- Click Done.
In the end, Corey’s Flow will look like the following screenshot:
Once everything looks good, perform the steps below:
- Click Save.
- Enter Flow Label the API Name will auto-populate.
- Click Show Advanced.
- API Version for Running the Flow: 51
- Interview Label: Email-to-Lead {!$Flow.CurrentDateTime}
- Click Save.
Almost there! Once everything looks good, click the Activate button.
Proof of Concept
From now onwards, if a sales rep sent an email to leadstosalesforce@gurukuloncloud.com then it forwards a copy to the email service address (email-to-case) which will create a case. On creation of case, Salesforce Flow will fire and it will create a lead and then delete the case.
- To test this functionality, sent out an email to leadstosalesforce@gurukuloncloud.com email address.
- Jump back to Salesforce and check Today’s Lead View and open the lead details page.
Formative Assessment:
I want to hear from you!
What did you learn from this post, is it relevant to you, and how will you modify the concepts taught in the post for your own business processes?
Make a post and tag me on Twitter @automationchamp, using #AutomationChampion.
Hi Rakesh,
Could you please help me on Step 4: Setup Email Forward from leadstosalesforce@gurukuloncloud.com to the Email Service Address ,how can I configure this step?
Here you go: https://help.salesforce.com/s/articleView?id=sf.customizesupport_configuring_routing_addresses.htm&type=5
In my case it is creating a case and not lead. It’s not deleting case as well.
1. Ensure you’ve designed the Flow correctly and Activated it.
2. Last option is to use Debug Log to find out why it is not working.
Hi Rakesh,
Email to lead is working for me as per your approach.
But my user case is a bit different:
I have two different email service address ( example: abc@gmail.com and xyz@gmail.com) for email to case routing information and if someone sends email to abc@gmail.com then case should be assigned to abc queue and if someone sends email to xyz@gmail.com then case should be assigned to xyz queue. Based on this in same way lead also should be assigned.
Can you please help me?
For case assignment use “Email-to-Case Routing Address” configuration.
One possible solution – For lead assignment use Async Path in Record-triggered Flow, grab case owner Id and use it when creating lead.
Can you Share the blog regrading : – Add a decision element if Salesforce is unable to find a user, then
– Assign the lead to some other user.
I am able to get the desired result of this but for our business case we email through the lead. I want the email that created the initial case to be related to the new lead. I was hoping I could just update the RelatedTo field on the email message but found out that will not work. Any thoughts or suggestions?
Excellent guide worked great. It fits my current business case perfectly much appreciated. If it becomes used more i may have to edit it slightly to accept from any email address.
Thanks for sharing this approach. Might consider trying it out but it seems to me that one of the key elements of a created Lead, the email address, is missing on the Lead. How can that be parsed from the initial email or the created Case and auto populated? Or perhaps I missed that in the tutorial? If not possible, then this solution requires manual intervention on every Lead that is created before it can be included in a Campaign.
Use SuppliedEmail from Case
The leads are generating if we send email from the email id that is a user in SF, when trying to send email from another email id that is not present as a user in SF then I am getting error “This error occurred when the flow tried to create records: INVALID_CROSS_REFERENCE_KEY: Owner ID: owner cannot be blank. ” how can this be resolved?
I wrote the blog post for allowing Sales Rep to Send emails to create leads via email-to-case. If you want to let any random email generate leads, you have to modify the logic.
– Add a decision element if Salesforce is unable to find a user, then
– Assign the lead to some other user.
I have done all the steps, case is getting created but the lead is not generated, while debugging I am getting this error “The triggering record didn’t meet the condition requirements, so the flow didn’t run.”
FlowApplication
3:50 PM (0 minutes ago)
to me
Debug the failed interview in Flow Builder.
Error element Create_a_Lead (FlowRecordCreate).
This error occurred when the flow tried to create records: INVALID_CROSS_REFERENCE_KEY: Owner ID: owner cannot be blank. You can look up ExceptionCode values in the SOAP API Developer Guide.
Make sure to assign the valid owner to the lead record. Second, make sure the sales rep (who has an account in Salesforce) sends the email, not a random person.
Hi Rakesh…GREAT tutorial. I built this without the schedule path (since it’s 0). Everything else is the same. However, the delete is executed before the lead is created, thus causing a flow error. I had to add a schedule path to delay the delete by 1 minute. Any idea why that is?
Yes, now it is possible to implement it using Flow. Is it possible for your to share the flow screenshots?
I am trying to do this. but its not working with this message below:
These issues prevent activation.
Before you can activate a flow that contains scheduled paths, specify the default workflow user in your org’s process automation settings. Then save your flow again and activate it.
Read this help article Designate Default Workflow User
Thank you for the fast reply. Rakesh Gupta. but I cannot find Workflow & Approvals under Create.
Look for Process Automation Setting
Thank you for the reply. I have already done it. but it keep saying that I cannot bring any userid from the begining. Could you let me know?
Please share the screenshot
which one should I share?
The error has just disappeared. but I am now getting this error when I run it.
somehow the error has just gone. but still getting error when I am running it on flow.
Thanks.
wow. it is working now~~. I guess it took a while to apply what I setup.
Thank you for your help. Rakesh Gupt.
Chris.
ps. do you have any email or trailhead that I can follow you?
Wow. I am glad to know that it works.
Feel free to reach out to me at contact@automationchampion.com
hey can please how to create without button can u share screen shots?
Hi Rakesh
I have created a flow and a process builder. Every thing works fine till the time I am not deleting the case record. The moment I try to delete the case record ,nothing works.
Please help me.
Hi Rakesh,
Thank you for your tutorial its very helpful.
How can we automate the process without creating an custom button.
Thanks,
Naresh
Here you go
1. Create a process on case object and look for Type=Lead
2. Pass the case record id to Flow
3. Use Flow to delete the record
Nice tutorial and work around for email-to-lead
I have a doubt, how can i pass the id of the case to the flow from workflow rule so that the flow will get executed for that particular case only?
I’m glad you liked it 🙂
First, create a variable in flow VarT_CaseID
Then modify the custom button URL to pass the case record ID to it, as mentioned below
/flow/case_to_email?retURL=00Q&{!VarT_CaseID}={!Case.Id}
Hi Rakesh ….I tried replicating this Flow but was unable to create leads althought was able to create case …
Make sure that you have assign value to all the required fields on Lead object