Salesforce Apex to Flow - For Starters with a Very Simple Example
APEX Code Into Flow
We have many times heard, implemented and worked with Flows. There are many sites and videos available which would give you an explanation of what flow is and how we can use it. Every site and video is worth your time if you have the zeal to learn, understand and excel.
So, you might now have a question, Why I am mentioning flows when there are many sites available!? Fair enough to have the question. I am here to just give my input on flows and how I tried to learn it in the first place before reaching any websites and videos.
I hope you would find this informative!
First and foremost, this content best suits those who have basic programming knowledge. I am not saying you must be a super coder or an experienced developer, What I mean is you can read and understand the APEX code. That's all it would take to understand anything and everything further.
Remember all of you I am also a beginner! So, let's dive in!
Flows:
We have got many definitions for flows, isn’t it? Don’t worry I am not going to pull out all the definitions available all over the internet. So, how do I define flows?
In layman's language "Flow is one of the feature/Applications made available in Salesforce which allows us to automate certain simple or complex actions or operations without writing any sort of code"
Simple isn't it!? Exactly! You use your mouse or cursor, click/type in the labels / API / Resource names, select the fields /conditions/ etc.… as per your requirement, connect them as per logic and boom ... you reach your solution. Remember we are just beginners with flows - so here we would want to learn slowly and go forward towards complexity. How are we going to do this: we would deconstruct Apex and construct Flow.
But then the question arises why are APEX, LWC and Aura when flows are there? Again, fair enough! But we will come to it later, maybe in this content or maybe later...First let's understand why we would prefer Flows instead of APEX via a simple example, which would give you the confidence to create and play with the flows at your own pace. All you need is to analyse your requirement accurately.
Before we see the example, just remember we need to understand the types of flows and the elements available:
Salesforce provides 2 Types of Flows
- Screen Flows
- Auto-Launched Flows
Auto-Launched flows are further categorized into 4 types:
- Record Triggered Flows
- Auto-Launched Flow (No Trigger)
- Scheduled Triggered Flow
- Platform Event Message Triggered Flows
Let’s quickly see what these types of flows mean.
-
Screen Flows:
provides User Interface, that supplies the input to the flow at runtime.
i.e., It requires the User Input.
Based on the Input provided by the user, it performs the operations.
-
Auto-Launched Flows:
Fire automatically based on the specified events.
i.e., It doesn't require any input from the end user.
The operations are performed in the background.
2.1. Record Triggered Flows:
These Flows can fire Automatically based on the DML Operations on the Salesforce Object records.
i.e., Insert, Update, and Delete operations will be fired and performed in the background.
These Flows can fire on "Before / After " operations.
2.2. Auto-Launched Flow (No Trigger):
These flows can't be fired automatically.
We must invoke these flows manually by using "Apex Programming".
2.3. Scheduled Triggered Flow:
These are used to implement the Scheduled Jobs, to get fired automatically based on periodic intervals.
It will execute the Scheduled Jobs asynchronously by placing inside the "Apex Queue".
Don't forget to check out: Learn All About Salesforce Apex Programming Language
2.4. Platform Event Message Triggered Flows:
It will fire the Flow, once it has received the Platform Event Message from the Lightning Component.
At this moment if you check the Flows they would not show Auto-launched flow, but your screen would display all the categories of auto-launched flow. I know, It’s much simplified for our convenience!
Flows would have the following 3 elements: (seen in Free=Form Layout on the left side and in Auto-Layout on clicking)
- Interaction elements: used to design User Interfaces to supply values to the flows at run-time
example: screen, sub-flow, ....
- Logic elements: used to perform intermediate operations on the input values given by the user at run-time
example: Conditional elements, Loops, collections...
- Data elements: used to interact with Salesforce objects and perform any DML operations on the object records.
Since you all understand the types of flows, and the elements let's go for the example.
Let’s say my requirement is:
Create an Account record and then create a related contact record only if the account meets the following conditions:
- Active = yes
- Annual Revenue > 500000
- Customer Priority = High
First, let us see what the Apex code would look like:
(Note: here I am implying that there exists an account record that already has matching criteria. Else we would need to write a code for Account record creation(WHICH IS NOT RECOMMENDED) or simply create an Account record, or use Data Loader – whichever suits you )
To Create a new Account record Via Apex code: This is just to show you the code but unless it is a must never code to create records of any objects as they would be considered HARDCODED which is never a best practice.
To create a Related contact record when the Account record is passed as a parameter.
Those who can read and understand code can easily understand the above code. Isn’t it? Perfect. Now let’s move to how we do the same operation via flows.
Let’s analyse the requirement:
Requirement:
Create an Account record and then create a related contact record only if the account meets the following conditions:
- Active = yes
- Annual Revenue > 500000
- Customer Priority = High
Analysis:
Object Name on which flow is based: Account
(You need to create an account first, Hence Object is Account)
What type of event are you foreseeing: After Insert
(After Insert because after the Account record is created/inserted proceed towards the creation of Contact record)
What type of flow you would choose: Record Triggered flow
(Record Trigger Flow because once an account is created and the criteria match then immediately you should trigger the Related Contact creation)
What are the flow conditions for related record creation:
1. Account Active = yes
2. Account Annual Revenue > 500000
3. Account Customer Priority = High
What is the action that needs to be performed after conditions are met: Create a Record
(Create Record because a Contact record needs to be created)
Hey, doesn’t this now seem easy to build a flow, now that you analysed the requirements properly? Good if you feel so, never mind if you didn’t. We all are still trying to learn.
Select Record Triggered Flow -> Create
You configure a trigger when you create an Account record Via Apex Code or Via Standard functionality. Here I am just showing standard functionality initially.
When to trigger create record -> when ALL CONDITIONS ARE MET. So, we just select the Field, the operator and the value. WE ARE DOING CLICK AND GO PROCESS, instead of typing the whole tedious code.
Since we want to perform an action to create a related record, we just select Actions and Related Records and just click done. Isn’t it simple like done in hardly half a minute!
Here I am using the Free – Form layout. You can use Auto-Layout also. Doesn’t matter.
Our next step is to Create a Related record I.e., Contact Record so we just simply drag the Create Records data element onto the canvas and it opens the screen where you just click and input the rest of the details.
Looking for Top Salesforce Consultants? Choose the best from here!
I have used the class name as Label Name and the API name would be auto-populated. If it doesn’t then just give underscore symbol after every word.
Since we are inserting a single related record for an account object here, I chose option One instead of Multiple under the How many records to create option.
Why are we using the “Use separate resources, and literal values” option: That’s because the fields in both objects vary. So we select this option. Go for another option if you want to simply copy all values of the Account record.
We just input the field values from the Account object.
To see the account object what we need to do is: in the value field just scroll to reach $Record Account and select the mapping fields. Remember the Record ID is always id. Never confuse it for Owner Id.
After all the required fields are mapped we simply click Done and connect the elements on the canvas. Click Save and give a proper name to flow and activate.
Whoa! How much time did it really take to build this flow? I will leave it up to you guys. Now it's time to test it, isn’t it?
OOPS! Related Contact is not created! Ohh No, Why? – Yeah you guessed it right! That’s because I haven’t entered the Active status as Yes while I created the Account Record.
Now If I update would it create the related Contact record? – No, because we didn’t configure the record trigger flow with the option “When a record is created or Updated”
Check out an amazing Salesforce video tutorial here: Apex Design Patterns: The Singleton Pattern in Salesforce
I made this error deliberately so that you can track where and why you went wrong. Now let’s do it right!
So that was a really simple example to demonstrate how flows take only a few minutes to work when compared to writing Apex code. Specifically, we use the CLICK AND GO Feature.
That’s exactly why Salesforce is encouraging FLOWS over writing Code. It is not only easy but a very powerful tool to make the life of each one of us easy.
Responses