DEV Community

Cover image for Salesforce Automation: Flow
Priyank Sevak
Priyank Sevak

Posted on

Salesforce Automation: Flow

I had a challenging task in front of me. I had recently worked on a user story in one of my sprints and one of my colleagues pinged me today telling me that there is a better and faster way to do this. Let me go over it with you to help you understand too.

TL;DR:

  • Problem: Needed to send automated emails based on Opportunity stage changes and owner.
  • Initial Approach: Writing Apex code triggers - complex and time-consuming.
  • Better Solution: Using Salesforce Flow - more efficient and requires minimal coding.
  • Flow Steps:
    1. Create Record-Triggered Flow for Opportunity object.
    2. Add "Decision" element to check for "Closed Won" stage.
    3. Create Email Templates for different scenarios.
    4. Use "Send Email Alert" action instead of "Send Email" due to recipient limitations.

Problem statement

As a user/agent I need to send the update regarding the newly created opportunity to the opportunity owner. I need to email the opportunity owner regarding the current stage of the opportunity and who updated the stage. If the stage has been set to "Closed won" the owner should receive a congratulatory email with the opportunity amount mentioned in the email.

My initial approach

Being a traditional web developer my instinct falls towards coding out the solution. I quickly laid out the following solution and mailed my colleague to check if that was the correct approach or not.

  1. Create an apex trigger for after insert and after update on the opportunity.
  2. Check for the current stage of the opportunity and determine which "email to be sent out"
  3. Set the email body and subject.
  4. Fetch the relative opportunity and their respective ownerId.
  5. Set the message ToAddress to the opportunity owner email address.
  6. send the email.

I knew there was some sharpening needed to my logic, so I approached my colleague. However, to my surprise, he responded with a laugh and said there is a much easier way that require almost no coding.

Salesforce Flow Automation

Salesforce could automate many tasks by using triggers, Process builders, and Flow.

Here are the type of flows available in salesforce and when we can use them.

REFERENCE: Trailhead FLow Builder Basics

Flow Basics

Renewed Approach: Record-Trigger Flow

1.Based on the options provided I quickly understood that I need to create Record-Trigger flow and configure it as below:

Configure Start

  • Object field: let us specify which object the flow needs to be triggered on.
  • We can trigger the flow: I have selected when "A record is created or updated" as I need to send the email when the Opportunity is created and updated.
  • Entry Condition: We can define entry condition which can determine whether to continue with the trigger or not. I can define conditions such as not triggering this flow if the opportunity amount is less than $1000.
  • Optimize the flow: Basically lets you select whether you want to trigger the flow before or after the object is created/updated. For sending emails it is advised to select after trigger.

2.I need to introduce a "decision" in the flow:

flow decision

  • In the "Outcomes" section you can define which outcome should fire which event or flow.
  • "Default outcome" is something that will be followed if no other match is found.
  • "closed won" I have introduced a new outcome here and determined based on the current stage of the opportunity in the conditions.

3.Create Email Template "Classic Email Template":

Image description

Image description

We hit a snag!!

We need to understand the 2 ways we can send an email using the Flow.

1.Using the Flow Action's "Send email Core Action":

Image description

This is the easiest and quickest way to send an email. However, Salesforce has a strict rule to include "Recipient ID" when using Email Template. and "Recipient ID" can only be "Lead, Contact or person account", while I want to send the email to Opportunity owner.

2.Using the "Email Alert":

Image description

"Email Alerts" are part of Salesforce Process automation and can be set up on any type or object, even custom objects. We can also define the Email template we want to use and the and can also select Recipients. Thus, getting rid of the limit we faced in the Flow action and can directly use the Email alert in the flow. We need to create another action for "Closed won opportunity" selecting the "Closed won email template" and adding recipient as above.

Continue the flow creation:

4.We can now go back to the flow we were working on and can now add "Send Email Alert":

Image description

  • This is also a straightforward way to send an email alert. We just need to make sure to select "Record ID" so that it can be used inside the "Email Template Merge Fields".
  1. And that's it, at the end, your flow should look something like the below:

Image description

-Make sure you debug and save the flow. You also need to activate the flow to make sure the record change will trigger this flow.

Output

Now, Whenever you Create a new opportunity and/or update the stage on the current opportunity you receive an email like below:

Image description

Image description

Conclusion:

Salesforce Flow offers a powerful and low-code solution for automating tasks like sending emails based on record changes. By leveraging Flows, you can streamline processes and improve efficiency compared to traditional coding methods.

Reference:

1.Flow Builder Basics Trailhead
2.Salesforce Geek on Youtube

Top comments (0)