In this blog we will learn Salesforce flow and how it is similar to apex coding.
Salesforce flow is a declarative way (Drag & Drop) of implementing an automation that is required for a business process alternative to code.
Blog covers types of flows, elements, resources and good comparison with a sample apex code.
Salesforce flow is a very powerful tool for admins/developers along with process builders and workflows. New admins/developers who are learning the Salesforce platform should put Salesforce flow as priority in their learning list.
Where do you find Salesforce Flow to start building one?
You should be Admin for an org to build salesforce flow, If you are admin please follow this path.
Go to “Setup” —-> Be on the “Home” tab —> Place the cursor on “Quick Find” —> Type “Flow”
Types of Flows?
We have different types in flow for different automation use cases. Salesforce is also improving their flows greatly and providing more options in future.
Here are the types: Click on “New Flow” you will find above screen.
Flows can be called via process builder, Auto launch flow will be called via process builder when where you define the criteria to launch the flow.
Deep dive in to Elements & Resources of flow:
When you select a type of flow and click next you will enter into a screen like below. This is where you will find tools and a canvas to build your logic.
As you can see in the above picture, canvas is where you build step by step process. Drag and drop these elements into the canvas to build.
When your building flows by drag and dropping elements, you will be automatically creating resources which are organized in the “Manager” section.
Resources are your variables, data and other materials which will help in achieving your automation process.
Understanding layers of elements & how can I relate them to apex code:
Interactions:
Logic:
In the below code snippet, you can observe that a variable has been declared and assigned with a value. Now, let’s see how it is similar in Assignments of flow.
String strName = 'My String'; //String variable declaration
Integer myInteger = 1; //Integer variable declaration
Boolean mtBoolean = true; //Boolean variable declaration
Operator value has more options based on the variable type.
{!myInteger} – this is variable which is a resource that has to be created like a record and the screen looks something like this:
While you are creating resources you can select resource type, there are few options in this and will be explained in my next blog or can get materials from salesforce.
for (APEX_Invoice__c objInvoice: PaidInvoiceNumberList) {
// Condition to check the current record in context values
if (objInvoice.APEX_Status__c == 'Paid') {
// current record on which loop is iterating
System.debug('Value of Current Record on which Loop is iterating is'+objInvoice);
// if Status value is paid then it will the invoice number into List of String
InvoiceNumberList.add(objInvoice.Name);
}
}
Loop will have a collection and then a variable where each record from the collection is taken. Now this single record variable will hold one record at a time.
if boolean_expression {
/* statement(s) will execute if the boolean expression is true */
} else {
/* statement(s) will execute if the boolean expression is false */
}
But in flow’s decision element along with your if else statements, there is a default outcome statement, where when none of the criteria is met it will be executed. This is how it might look in full conditions.
Data:
Data in flows are very similar to your Apex SOQL operations like getting data and later you have your DML operations like Update, Delete, Create.
Data can be collected in with specific conditions or just get all the data and the same applies to your DML operations like Update, Delete, Create.
Re-Imagining Apex code in Salesforce flow:
We will take a piece of code and re-imagine that same code in flow. I will be using “Auto launch flow”.
// Initializing the custom object records list to store the Invoice Records created today
List PaidInvoiceNumberList = new List ();
// SOQL query which will fetch the invoice records which has been created today
PaidInvoiceNumberList = [SELECT Id,Name, APEX_Status__c,Paid_Date__c FROM APEX_Invoice__c WHERE
CreatedDate = System.today()];
// List to store the Invoice Number of Paid invoices
List InvoiceNumberList = new List ();
// This loop will iterate on the List PaidInvoiceNumberList and will process each record
for (APEX_Invoice__c objInvoice: PaidInvoiceNumberList) {
// Condition to check the current record in context values
if (objInvoice.APEX_Status__c == 'Paid') {
// if Status value is paid then it will update the paid date to systems today's date
objInvoice.Paid_Date__c = System.today();
// current record on which loop is iterating
System.debug('Value of Current Record on which Loop is iterating is'+objInvoice);
// if Status value is paid then it will the invoice number into List of String
InvoiceNumberList.add(objInvoice);
}
}
if(InvoiceNumberList.size() > 0) {
Update InvoiceNumberList;
}
Decoding of Apex:
// SOQL query which will fetch the invoice records which has been created today
PaidInvoiceNumberList = [SELECT Id,Name, APEX_Status__c,Paid_Date__c FROM APEX_Invoice__c WHERE CreatedDate = System.today()];
We will have to get invoice records from the Invoice object with a condition as mentioned in SOQL query. Drag the “Get Records” element into the canvas and give information accordingly. As we are getting all the records, flow will automatically create a record collection and store all field values or just like in the above SOQL query you can choose specific fields. I choose Automatically store all fields.
// This loop will iterate on the List PaidInvoiceNumberList and will process each record
for (APEX_Invoice__c objInvoice: PaidInvoiceNumberList) {
// Condition to check the current record in context values
if (objInvoice.APEX_Status__c == 'Paid') {
// if Status value is paid then it will update the paid date to systems today's date
objInvoice.Paid_Date__c = System.today();
// current record on which loop is iterating
System.debug('Value of Current Record on which Loop is iterating is'+objInvoice);
// if Status value is paid then it will the invoice number into List of String
InvoiceNumberList.add(objInvoice);
}
In this block of code they are initiating a loop and checking each record in the loop where status is “Paid.” Let’s see how it is done in flow.
First we have the Loop element and define record collection variable and loop variable which hold a single record at a time.
Now, we will have a Decision element to check for records with status.
// if Status value is paid then it will update the paid date to systems today's date
objInvoice.Paid_Date__c = System.today();
// if Status value is paid then it will the invoice number into List of String
InvoiceNumberList.add(objInvoice);
For the records where if condition is satisfied status is equal to paid are updated with paid date field equals to today’s date and add the records to a list called “InvoiceNumberList” here these will be a record collection resource. Let’s see how this is executed in flow.
First we have to update the Paid Date field to today’s date and we have to create a resource “InvoiceNumberList” record collection and then use assignment to add the records which meet status paid into the collection.
Now we have to update the list as per the code, it is checking if the list size is greater than zero or not, in flow you can check if the list is not null.
if(InvoiceNumberList.size() > 0) {
Update InvoiceNumberList;
}
Drag & drop the Decision element.
Now drag & drop the Update Records element, In this element you can select the whole list and update the records.
Now lets see how flow looks.
System.debug('Value of Current Record on which Loop is iterating is'+objInvoice);
Just like in Apex code where you can debug. Same feature is available in flows, before activating the flow i recommend you to debug your flow.
Conclusion:
Hope this give’s good understanding of Salesforce flows. Will meet you guys next time with another topic.