+ Start a Discussion
k practicek practice 

How write a trigger on account name duplicate based on 2 alerts like creation and Existing?

MithunPMithunP
Hi k practice,

Here is the sample code.
trigger accTrig on Account (after insert, after update) {
   
      list<Account> accList = [select id,name from account];
      for(Account accn : trigger.new){
          for(Account acc : accList){
              if(accn.id != acc.id){
                    if(accn.name == acc.name){
                       accn.name.addError('Duplicate Account Name');
                    }
              } 
          }
      }

}
Best Regards,
Mithun.
 
Vijay NagarathinamVijay Nagarathinam
Hi @K practice,

Try this code it will be work and it is bulkified 
 
trigger duplicateAccName on Account (before insert,before update)
{
Map<string,Account> accMap = new Map<string,Account>();
set<string> accSet = new set<string>();
    for(Account acc : Trigger.new)
        accSet.add(acc.name);
    for(Account a : [select id,name from Account where name in : accSet])
        accMap.put(a.name,a);
    for(Account acc1 : trigger.new)
        {
        if(accMap.containskey(acc1.name))
        acc1.addError('Account name Already Exist!!');
        }
}

 
k practicek practice

Hi Sir,
This is my requirement
1) When a new account is being created, test the account name against all other account names already in the system. If there is an exact match, then prevent the save and pop this alert:
> “Duplicate records are exist”

2)- When saving an existing account, on save, also run the test to look for an exact name match on the account name (ensure you don’t find the same account as the one being saved). In this situation, if there is an exact match, we will still allow the save, but we’ll pop this alert as a warning:
> “There is another Account with the same name as this one. Please navigate to the Account tab, and in the Tools section on that page, execute the Account Merge in order to clean up this duplicate. If you are not the account owner of both accounts, please contact the account owner(s) and ask them to do this merge.”

I tried Like below .How to apply second statement in below trigger help me...

trigger AccountDuplicateTrg on Account (before insert,after update) {
    List<Account> all=Trigger.New;
    List<Id> accids=new List<Id>();
    List<String>  listOfAccountnames=new List<String>();
    for(Account a1:all)
    {
        listOfAccountnames.add(a1.name);
        accids.add(a1.id);
        //accids.add(a1.id);
        //System.debug(accids);
    
    }
    List<Account> accs=[SELECT id,name FROM Account WHERE Name IN : listOfAccountnames AND Id IN : accids];
    System.debug(accs);
    Map<String,Account> Mp=new Map<String,Account>();
    
    for(Account a2:accs)
    {
        Mp.put(a2.name,a2);
        
    }
    
    for(Account a:Trigger.new){
        if(Mp.get(a.name) != null){
            a.addError('Duplicate records are exist');
        }
    }
     for(Account a3:accs)
    {
        Mp1.put(a3.Id,a3);
        
    }
     for(Account Oaccount:Trigger.new){
        if(Mp1.get(Oaccount.Id) != NULL){
            Oaccount.addError('There is another Account with the same name as this one. Please navigate to the Account tab, and in the Tools section on that page, execute the Account Merge in order to clean up this duplicate. If you are not the account owner of both accounts, please contact the account owner(s) and ask them to do this merge.');
        }
    }
}

please help me.............