There are many scenarios where we need to create/update records. If there is complex business logic where we need to create and update records based on some certain conditions, then generally, Developer go with Apex Triggers and starts writing hundreds lines of code. Obviously, that is not a good practice.
Developers forgot the powers of lightning flows where we can build complex business logics with some clicks and drags. So, as part of this article; I am going to explain how can we make good use of lightning flow and Process builder.
Here’s the use case:
Let’s say you are an investment corporation selling some investment plan to the customer. Your sales rep is created an opportunity. On opportunity, there is a custom field of type long text area say “Remarks”. In this, Remarks field, Sales Rep is storing special instructions for business while working on opportunity.
Now the pain problem for sales rep is to track the Remarks field’s data like when and what contents has been changed. To track this change Business Architect has suggested a solution like when opportunity is created/updated, copies the data from Remarks field in the Opportunity Object to a Note record if Remarks field is changed and Note shall be associated with the Opportunity.
To achieve this functionality, we will use Process Builder (PB) and Lightning Flow. PB will invoked when Remarks field is changed and from PB, we will invoke auto launched flow which will be used for to create a record under note and attachment related list.
Process Builder: In this, we will define below criteria:
ISCHANGED([Opportunity].Remarks__c) || (ISNEW() && NOT(ISBLANK([Opportunity].Remarks__c) )
Now call flow in immediate action, first we will create auto Launched Flow in Flow builder. Below are the steps to create Auto Launched Flow in Flow Builder.
- Go to Setup>>Build>>Create>>Workflow & Approvals>>Flow.
- Click on New Flow button. You will see following screen.
- Click on Manager and Create following resources.
- recordId: This variable is a standard variable and will be used to store the current id of the opportunity record.
- TextTitle: This will be the resource formula and will be used to set Title for Note record.
- varOppRecord: We will create record variable and this will be used to store the opportunity record.
- Get Opportunity: We will use “Get Records” Data Element to get the opportunity record. Below screenshot shows how it is final look like.
- Create Notes: We will use “Create Records” Data Element to create the Note record under opportunity related list. Below screenshot shows how it is final look like.
- Connect all the elements and save the flow.
Final Flow: Below screenshot shows that how final flow look like.