Trigger to check duplicate name to custom object in Salesforce | how to prevent duplicate records based on multiple fields through apex trigger in Salesforce

2,335 views


Hey guys, today in this post we are going to learn about How can we prevent duplicate name insert on custom object in Salesforce.

Real time scenarios:- Write a trigger on custom object When a Object (childObjTrigger__c) is being created, if a record exists with the same EmployeeName (EmployeeName__c), the trigger should throw an error.

Create a custom object with custom field name as EmployeeName

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

 

Custom Object:- childObjTrigger__c >> custom field:- EmployeeName__c (Text
type)

Files we used in this post example:-

childTriggerhandler.apxt Apex Class Trigger It is used for prevent duplicate record creation with same name
childObjHandler.apxc Apex Class Handler It is call into cildTriggerhandler.apxt file.

Custom Child Object:- childObjTrigger__c

Custom Field:- EmployeeName__c

Custom Child Object with Custom Field It is custom object with custom field used for display an error if same name being inserted.

You can download file directly from github by click here

Final Output

w3web.net -- trigger to check for duplicates

 

You can download file directly from github by Click Here.

 
 

Other related post that would you like to learn in Salesforce

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

Step 1:- Apex Class Trigger : childTriggerhandler.apxt

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

childTriggerhandler.apxt [Apex Class Trigger]

  1.    TRIGGER childObjTriggerHandler ON childObjTrigger__c (BEFORE INSERT, after INSERT) {
  2.  
  3.     IF(TRIGGER.isBefore){
  4.         IF(TRIGGER.isInsert){
  5.             system.debug('I am inside before insert');  
  6.             childObjHandler.childDuplicate(TRIGGER.new);
  7.         }
  8.     }
  9.     ELSE IF(TRIGGER.isAfter){
  10.         IF(TRIGGER.isInsert){
  11.             //system.debug('I am inside after insert');  
  12.             system.debug('Record inserted successfully');              
  13.         }        
  14.     }    
  15. }

Step 2:- Apex Class : childObjHandler.apxc

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

childObjHandler.apxc [Apex Class Handler]

  1.    public class childObjHandler {
  2.  
  3.     //Do NOT allow duplicate Employee name
  4.     public static void childDuplicate(List<childObjTrigger__c> dupChildRec){        
  5.         Set<String> empExtStr = NEW Set<String>();
  6.         List<childObjTrigger__c> empList = [SELECT Id, Name, EmployeeName__c, childLookup__c, Status__c FROM childObjTrigger__c WHERE EmployeeName__c !=NULL];
  7.  
  8.         FOR(childObjTrigger__c empStr:empList){
  9.             empExtStr.add(empStr.EmployeeName__c);
  10.         }  
  11.  
  12.         FOR(childObjTrigger__c childTr:dupChildRec){
  13.             IF(empExtStr.contains(childTr.EmployeeName__c)){
  14.                 childTr.EmployeeName__c.addError('Do not allow duplicate');
  15.             }
  16.         }
  17.     }
  18. }

Further post that would you like to learn in Salesforce

 

 

FAQ (Frequently Asked Questions)

What is Apex trigger in Salesforce?

Apex triggers enable you to perform custom actions before or after events to records in Salesforce, such as insertions, updates, or deletions. Just like database systems support triggers, Apex provides trigger support for managing records.

What is the use of Apex triggers?

Apex triggers enable you to perform custom actions before or after changes to Salesforce records, such as insertions, updates, or deletions. A trigger is Apex code that executes before or after the following types of operations: insert. update.

What is the difference between Apex and trigger in Salesforce?

Classes consist of other classes, user-defined methods, variables, exception types, and static initialization code A trigger is Apex code that executes before or after specific data manipulation language (DML) events occur, such as before object records are inserted into the database, or after records have been deleted.

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