function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aruna Dhavaleswarapu 4Aruna Dhavaleswarapu 4 

avoid duplication of variable through flows

How to avoid duplication of variable through flows
Best Answer chosen by Aruna Dhavaleswarapu 4
NagendraNagendra (Salesforce Developers) 
Hi Aruna,

Flow, As the word says, flow easily flows to fulfill the requirements with ease.☺  Many think of app exchange solutions and source code to avoid the duplicate record in Salesforce, But the method I'm going to explain here is completely different and very simple. It helps you to reduce your code and can easily rebuild for other objects with different field logic with simple clicks.

Let's jump into the solution, we will use flow to check duplicate record logic and use apex trigger to initiate the Flow whenever a record is created or updated.

Visual WorkFlow:[Click]

First, we will create the Flow to check the duplicate records based on the name and store the result in the variable. In this example, we will use the Account object for the dupe check.

Step 1: Create a new visual flow with the Name ‘DuplicateAccountCheck'
Step 2: Click and drag the RecordLookup into the flow window and Name it as ‘DuplicateCheck'
Step 3: Select the object as ‘Account'
Step 4: In the Fields select “ID" and condition as "does not equal” in the value and create new variable ‘varAccountId’.
Step 5: Select Field as “Name” and Operator as “equal” in the value and create new variable ‘varAccountName'.


Note: Set Variable Input/Output Type as InputOnly
User-added image
Step 6: Select the field as ‘Name’ and under the variable create new variable ‘varReturnAccount'.
Step 7: Select the checkbox Assign a null value to the variable if no records are found and save it.
User-added imageWhenever this flow is initiated with the parameter Account name and Id then it will check for the Account record based on the name, if any record matches then it will store in the variable ‘varReturnAccount’. So now logic to check duplicate account record is ready, let's move on to the next logic.

Trigger:[Code]

We easily covered the major logic for the duplicate check without writing a single line of code using flow, all we going to do now is call our flow whenever a record is created or updated from the trigger and display an error message if it has a duplicate record. Calling flow in an apex class is a straightforward approach, below is the apex trigger code which will call your flow.
trigger DuplicateCheck on Account (before insert,before update) {

  for(Account acc: Trigger.New)
  {
    Map<String, Object> params = new Map<String, Object>();
    params.put('varAccountName',acc.Name);
    params.put('varAccountId',acc.Id);
        
    Flow.Interview.DuplicateAccountCheck duplicateAccountCheck = new Flow.Interview.DuplicateAccountCheck(params);
    duplicateAccountCheck.start();
 
    // Obtain the results
    String returnValue = (String) DuplicateAccountCheck.getVariableValue('varReturnAccount');
    if(returnValue != null && returnValue != '')
      acc.Name.Adderror('Duplicate Account Found with the same Name: '+returnValue);
  }
}
Whenever a new record is inserted or updated, the trigger will call the flow which will perform the duplicate check and store it in the variable, if the variable is not null then it will throw the error message for that particular record. Now we linked the flow and trigger, which helps non-developers to build solutions for the complex problem with the more Clicks and less Code.

Hope this helps.

Please mark this as solved if it's resolved.

Thanks,
Nagendra