+ Start a Discussion
viswsanathviswsanath 

How can I Avoid duplicate names using triggers

hamshuhamshu

Try This,

 

trigger DuplicateLead on lead (before insert,before update)
{
 
         Set<string> lastname= new Set<string>();
   
      
        
         for(lead lead : Trigger.new)
         {
           lastname.add(lead .lastname );
         }

       
       List<Lead > duplicateleadList = [Select lastname From Lead where lastname = :lastname];

       Set<string > duplicateLeadIds = new Set<string >();

       for(lead dup: duplicateleadList )
       {
         duplicateLeadIds .add(dup.lastname);
       }

       for(lead s : Trigger.new)
       {
            if(s.lastname!=null)
            {
               if(duplicateLeadIds.contains(s.lastname))
               {
                 s.addError('Record already exist with same Name');
               }
            
            }
       }
}

 

 

Santhosh AloorSanthosh Aloor

try this below example

 

trigger DuplicateRecord on Static_Activity__c (before insert)

{  

  List<Static_Activity__c > StaActiList = [Select Name, Activity_for_Company__c, User_Name__c from Static_Activity__c ];  

for (Static_Activity__c staActnew : Trigger.new)

  {      

    for(Static_Activity__c sa: StaActiList)

        {         

               if(sa.User_Name__c == staActnew.User_Name__c)         

                  {              

                    staActnew.User_Name__c.addError('Duplicate record on this User Name');        

                  }     

       } 

  }

}

viswsanathviswsanath
Thanks for Hamshu
Arunkumar.RArunkumar.R

To check multiple condition to avoid duplication just go through the below link,

 

Here I have checked firstname, lastname and Email fields to avoid duplicate records

 

http://salesforcekings.blogspot.in/2013/11/salesforce-deduplication-trigger-on.html

 

If you found this answer helpful to you... Please accept this as a Solution and  give kudos by clicking on the star icon.

 

Thanks and Regards,

Arunkumar.R | Salesforce Certified Developer

hamshuhamshu

If you found this answer helpful to you... Please accept this as a Solution and  give kudos by clicking on the star icon.

database triggerdatabase trigger
When i was trying to edit the record and update the trigger was firing could you please check and update
 
Tushar MahadikTushar Mahadik
Thanks Santhosh Aloor

It works for me
GhulamGhulam
Hi Frnds,
please help me... to resolve my problem
Avoid Duplicate Name Only on master in Master-Detail relationship
Hi,
I have two custom object Claim__c(master) and Labour_Cost__c(detail).
They have master detail relationship.
I want to create a Trigger which prevent duplicate Name(standard field which is created default when we create any custom object) only on master object.
but we can multipe record with same Name from detail object and we select it from lookup field (Claim_Name__c) on detail object.
plz write a trigger for same.
for example: I have a record on Master Object with Name='S1 Claim' .
if i create a new record with Name 'S1 Claim' then it will display an error 'record already exists with same name'.
But,I can create multiple record with Name 'S1 Claim' from detail object.

Thanks,
Ghulam
 
Alicia Chen 2Alicia Chen 2
https://success.salesforce.com/answers?id=90630000000hzBlAAI
preetam dubey 1preetam dubey 1
To prevent duplicate account creation or lead creation .Lets follow salesforce standard method:-

Lets say prevent duplicate account creation :-

Create a text field :- "say X".
                               Select checkboox unique.*********
                               No need to expose in any page layout.
Create a workflow rule :- Name it as "say Y "
                                    Created and every time it is edited 
                                    ---Criteria :Account name is not equal to null  .SAVE
                                    Action:Field update :--name the field update as "say A"
                                    From drop down choose text field "X".
                                    In formula editior : Name(NOTE:-as account name api is name ).
                                    SAVE .ACTIVATE .


Now it is ready ..

Create account >> Go to account tab in header>>NEW>> in account name put "TEST1"

Create another account with same account name"TEST1".

Hope above method helps you..



*********NOTE :- but many of you are thing what if i create 2nd account with name 'test1'--DO it prevent that time also.
So answer is it is as per business requirement :
If business wants : name should unique subjective to case insensitive means if TEST1 and test 1 should be treated as same account than check checkbox --->Treat "ABC" and "abc" as duplicate values (case insensitive) while creating text field "X"

If business wants : name should unique subjective to case sensitive means if TEST1 and test 1 should be treated as different account than check checkbox --->Treat "ABC" and "abc" as different values (case sensitive) while creating text field "X".



         
 
amit bhumburamit bhumbur
Thank you so much 
Santhosh Aloor
raj_sfdccraj_sfdcc
Hi ,
Below post can be used to avoid Duplicate Fields by using trigger :

Here i have used Email for my requirement .Please use Name field in the place of Email

 
trigger DemoTrigger on Author__c (before insert,before update) {
    List<Author__c> accList=new List<Author__c>([select Author_Email__c from Author__c]);
    map<String,Author__c> accmap=new map<String,Author__c>();
    for(Author__c acc:accList){
        accmap.put(acc.Author_Email__c,acc);
    }
 
    if(Trigger.isinsert&&Trigger.isbefore){
        for(Author__c acc:Trigger.new){
            if(accmap.get(acc.Author_Email__c)!=null){
                acc.adderror('Email already exists');
            }
        }
    }
}


Please find the below post further information:

Avoid Duplicate for Insert Operation By Using Apex Trigger (https://salessforcehacks.blogspot.com/2019/12/avoid-duplicate-fields-using-apex.html)

And the below Post explaines how to avoid Duplicate Fields by updating the above code :

Avoid Duplicates for both insert and Update Operation (https://salessforcehacks.blogspot.com/2019/12/avoid-duplicate-fields-using-apex_21.html)


I have created insert and update operation seperately  to make you understand Clearly.