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:-
- 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 →
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
From Developer Console >> File >> New >> Apex Trigger
RestrictDuplicatePhoneOnContactTrigger.apxt [Apex Trigger]
TRIGGER RestrictDuplicatePhoneOnContactTrigger ON Contact (BEFORE INSERT,BEFORE UPDATE) {
IF(TRIGGER.isBefore){
system.debug('Event before trigger');
IF(TRIGGER.isInsert){
duplicatePhoneOnContactCtrl.checkDuplicatePhone(TRIGGER.new);
}
ELSE IF(TRIGGER.isUpdate){
duplicatePhoneOnContactCtrl.checkDuplicatePhone(TRIGGER.new);
}
}
ELSE IF(TRIGGER.isAfter){
system.debug('trigger after event');
}
}
Create Apex Class Controller →
Step 2:- Create Apex Class : duplicatePhoneOnContactCtrl.apxc
From Developer Console >> File >> New >> Apex Class
duplicatePhoneOnContactCtrl.apxc [Apex Class Controller]
public class duplicatePhoneOnContactCtrl {
public static void checkDuplicatePhone(List<Contact> conObjItem){
Map<String,String> conMapItem=NEW Map<String,String>();
set<String> accStr=NEW set<String>();
FOR(Contact con:conObjItem){
IF(con.AccountId!=NULL)
accStr.add(con.AccountId);
}
FOR(Contact con:[SELECT Id,Name,Phone,AccountId FROM Contact WHERE AccountId=:accStr]){
conMapItem.put(con.Phone,con.AccountId);
}
FOR(Contact con:conObjItem){
IF(!conMapItem.isEmpty() && conMapItem.containskey(con.Phone)){
con.addError('Do not allow duplicate Phone number, this phone number is alread exist on related account');
}
}
}
}
Further post that would you like to learn in Salesforce
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
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 |