Apex trigger on Account to avoid creation of duplicate record if the account with same phone exists in the system in Salesforce | Write a trigger on Account to Prevent the user to create duplicate Account based on Phone if Phone number is already exist in Salesforce

5,161 views


Hey guys, today in this post we are going to learn about how to write Apex trigger on Account to avoid creation of duplicate record if the account with same phone exists in the system in Salesforce.

A Trigger is a segment of Apex code which executes before or after inserting or modifying a Salesforce record based on the condition provided.

There are different types of triggers based on the action going to be performed. They are Before Triggers and After Triggers. To know more details about Apex Trigger, Click Here.

Note:: โ€“ You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
   

 

 

Final Output โ†’

trigger on account to prevent duplicate records -- w3web.net
 

 

You can download file directly from github by Click Here.

 

Other related post that would you like to learn in Salesforce

 

  • Find the below steps โ–พ

 

Create Apex Trigger โ†’

Step 1:- Create Apex Trigger : checkDuplicationPhoneAccTrigger.apxt

From Developer Console >> File >> New >> Apex Trigger

Note:: โ€“ You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 

checkDuplicationPhoneAccTrigger.apxt [Apex Trigger]

  1.     TRIGGER checkDuplicationPhoneAccTrigger ON Account (BEFORE INSERT,after INSERT) {
  2.     IF(TRIGGER.isBefore){
  3.         system.debug('trigger before trigger'); 
  4.  
  5.         IF(TRIGGER.isInsert){
  6.             set<String> accPhone=NEW set<String>();
  7.             FOR(Account a:TRIGGER.new){
  8.                 accPhone.add(a.Phone);
  9.                 System.debug('Account phone number is ' +accPhone);
  10.  
  11.             }
  12.             set<String> phoneAccData=NEW set<String>();
  13.             FOR(Account a:[SELECT Id,Name,Phone FROM Account WHERE Phone IN:accPhone]){
  14.                 phoneAccData.add(a.Phone);
  15.  
  16.             }
  17.             FOR(Account a:TRIGGER.new){
  18.                 IF(phoneAccData.contains(a.Phone)){
  19.                     a.addError('Do not allow insert duplicate phone number');
  20.                 }
  21.             }
  22.         }
  23.     }
  24.  
  25.     ELSE IF(TRIGGER.isAfter){
  26.         system.debug('trigger after trigger');        
  27.     }
  28.  
  29.  
  30. }

 

 

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

How do you prevent creating duplicate records in Salesforce using trigger?

Preventing duplicate records in Salesforce based on a single field can be achieved using a Set that can store the values of that specific field from all existing records and compare it with the list of new records that are going to be inserted.

How can you prevent duplicate leads from being created?

Block sales reps from creating duplicate leads. In the Standard Lead Duplicate Rule, select Block instead of Allow. With the Standard Lead Duplicate Rule set to block duplicates, a rep can click to view duplicates of leads but can't save a new lead.

What is matching rule and duplicate rule in Salesforce?

Matching rules and duplicate rules work together to ensure that your sales teams work with data that's free of duplicates. Before your reps save new and updated records, matching rules and duplicate rules provide warnings of potential duplicates. You manage matching rules and duplicate rules in Setup.

Related Topics | You May Also Like

 
Note:: โ€“ You will get an email, so put correct email and mobile number and BEGIN YOUR JOURNEY from Today!
 
 
  

Our Free Courses โ†’

๐Ÿ‘‰ Get Free Course โ†’

๐Ÿ“Œ Salesforce Administrators

๐Ÿ“Œ Salesforce Lightning Flow Builder

๐Ÿ“Œ Salesforce Record Trigger Flow Builder

๐Ÿ‘‰ Get Free Course โ†’

๐Ÿ“Œ Aura Lightning Framework

๐Ÿ“Œ Lightning Web Component (LWC)

๐Ÿ“Œ Rest APIs Integration



Hi, This is Vijay Kumar behind the admin and founder of w3web.net. I am a senior software developer and working in MNC company from more than 8 years. I am great fan of technology, configuration, customization & development. Apart of this, I love to write about Blogging in spare time, Working on Mobile & Web application development, Salesforce lightning, Salesforce LWC and Salesforce Integration development in full time. [Read full bio] | | The Sitemap where you can find all published post on w3web.net

Leave a Comment