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
Louis XavierLouis Xavier 

How to create a trigger to validate a duplication fields in custom objects, before insert event

Best Answer chosen by Louis Xavier
Khan AnasKhan Anas (Salesforce Developers) 
Hi Louis,

I trust you are doing very well.

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Custom Object: Book__c
 
trigger BookDuplicate on Book__c (before insert, before update) {
    
    Set<String> names = new Set<String>();
    
    for(Book__c acc : trigger.new){
        names.add(acc.Name);        
    }
    
    //get list of Books with input name
    List<Book__c> accList = new List<Book__c>();
    accList = [SELECT Id, Name FROM Book__c WHERE Name IN: names];
    
    for(Book__c ac : trigger.new){
        /*check if it is insert event or not
         * if insert event then returns true else returns false
         * */
        if(trigger.isInsert){
            /*check dulicate Book list size 
             * if size is 0 it means no duplicate Books
             * */
            if(accList.size() > 0 )
                ac.Name.addError('Book already exists with this name');
        }
        
        /*check if it is update event or not
         * if update event then returns true else returns false
         * */
        if(trigger.isUpdate)
        {
           for(Book__c oldaccount :trigger.old) {
               /*when updating an Book check that Book name is changed or not
                * Here ac.Name is new value entered and oldAccount.Name name represents old name of Book
                * if name is changed then check given Book name is already exist or not
                **/
               if(ac.Name!=oldAccount.Name && accList.size()!=0) {
                   ac.Name.addError('Book already exists with this name');
               }
           }
        }
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Louis,

I trust you are doing very well.

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Custom Object: Book__c
 
trigger BookDuplicate on Book__c (before insert, before update) {
    
    Set<String> names = new Set<String>();
    
    for(Book__c acc : trigger.new){
        names.add(acc.Name);        
    }
    
    //get list of Books with input name
    List<Book__c> accList = new List<Book__c>();
    accList = [SELECT Id, Name FROM Book__c WHERE Name IN: names];
    
    for(Book__c ac : trigger.new){
        /*check if it is insert event or not
         * if insert event then returns true else returns false
         * */
        if(trigger.isInsert){
            /*check dulicate Book list size 
             * if size is 0 it means no duplicate Books
             * */
            if(accList.size() > 0 )
                ac.Name.addError('Book already exists with this name');
        }
        
        /*check if it is update event or not
         * if update event then returns true else returns false
         * */
        if(trigger.isUpdate)
        {
           for(Book__c oldaccount :trigger.old) {
               /*when updating an Book check that Book name is changed or not
                * Here ac.Name is new value entered and oldAccount.Name name represents old name of Book
                * if name is changed then check given Book name is already exist or not
                **/
               if(ac.Name!=oldAccount.Name && accList.size()!=0) {
                   ac.Name.addError('Book already exists with this name');
               }
           }
        }
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Louis XavierLouis Xavier
Thanks Khan.