Write a trigger on Contact to Prevent the user to create duplicate Contact based on Phone if Phone number is already exist on related account in Salesforce | Apex trigger on Contact do not allow to create duplicate contact based Phone number if the contact with same phone exists in related account in Salesforce

3,963 views


Hey guys, today in this post we are going to learn about how to prevent the user to create duplicate Contact based on Phone if Phone number is already exist on related account in Salesforce Apex Trigger.

Apex can be invoked by using triggers. Apex triggers enable you to perform custom actions before or after changes to Salesforce records, such as insertions, updates, or deletions.

There are two types of triggers:-

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

 
  • Before triggers are used to update or validate record values before they’re saved to the database.
  • After triggers are used to access field values that are set by the system (such as a record’s Id or LastModifiedDate field), and to affect changes in other records, such as logging into an audit table or firing asynchronous events with a queue. The records that fire the after trigger are read-only.

Triggers can also modify other records of the same type as the records that initially fired the trigger. To know more about apex trigger, Click Here →

 

 

Final Output →

trigger duplicate Contact based on phone -- 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 : RestrictDuplicatePhoneOnContactTrigger.apxt

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

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

RestrictDuplicatePhoneOnContactTrigger.apxt [Apex Trigger]

  1.    TRIGGER RestrictDuplicatePhoneOnContactTrigger ON Contact (BEFORE INSERT,BEFORE UPDATE) {
  2.     IF(TRIGGER.isBefore){
  3.         system.debug('Event before trigger');
  4.         IF(TRIGGER.isInsert){
  5.             duplicatePhoneOnContactCtrl.checkDuplicatePhone(TRIGGER.new);
  6.         }
  7.         ELSE IF(TRIGGER.isUpdate){
  8.             duplicatePhoneOnContactCtrl.checkDuplicatePhone(TRIGGER.new);
  9.         }
  10.     }
  11.     ELSE IF(TRIGGER.isAfter){
  12.         system.debug('trigger after event');   
  13.  
  14.     }  
  15.  
  16. }

 

Create Apex Class Controller →

Step 2:- Create Apex Class : duplicatePhoneOnContactCtrl.apxc

From Developer Console >> File >> New >> Apex Class

duplicatePhoneOnContactCtrl.apxc [Apex Class Controller]

  1.    public class duplicatePhoneOnContactCtrl {
  2.  
  3.     public static void checkDuplicatePhone(List<Contact> conObjItem){
  4.  
  5.         Map<String,String> conMapItem=NEW Map<String,String>();
  6.         set<String> accStr=NEW set<String>();
  7.         FOR(Contact con:conObjItem){
  8.             IF(con.AccountId!=NULL)
  9.                 accStr.add(con.AccountId);
  10.         }
  11.  
  12.         FOR(Contact con:[SELECT Id,Name,Phone,AccountId FROM Contact WHERE AccountId=:accStr]){
  13.             conMapItem.put(con.Phone,con.AccountId);
  14.         }
  15.         FOR(Contact con:conObjItem){
  16.             IF(!conMapItem.isEmpty() &&  conMapItem.containskey(con.Phone)){
  17.                 con.addError('Do not allow duplicate Phone number, this phone number is alread exist on related account');
  18.             }
  19.         }
  20.  
  21.     }
  22.  
  23. }

 

trigger duplicate Contact based on phone -- w3web.net
 

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 do you prevent duplicate records in Access?

You can prevent duplicate values in a field in an Access table by creating a unique index. A unique index is an index that requires that each value of the indexed field is unique.

Does HashMap allow duplicate keys?

HashSet doesn't allow duplicate values. HashMap stores key, value pairs and it does not allow duplicate keys.

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