+ Start a Discussion
aswanijagadesh1.397398535585991E12aswanijagadesh1.397398535585991E12 

insert new account by using trigger?

hai friends 
if account name is alerady exit then display error message ,other wise insert new account. for this i wrote like this
trigger DuplicateAcc on Account(after insert) {
    List<Account> acc=[select id,name from account];
    public account acc1;
    //system.debug('1111111'+acc.size());
    for (Account acct:Trigger.new) {   
     for(Account a:acc)
     {
       if(acct.name==A.name)
         {          
         acct.addError('ERROR: there is already an identical record Name in Data Base: <a href=\'https://na1.salesforce.com/'  + a.id + '\'+ target="_blank">'+ a.id+'</a>',false);
         return;
         }
         //acc1=new account(name=acct.name);        
         //insert acc1;
        
        }
         // system.debug('********'+acct.name);  
        // acc1=new account(name=acct.name);        
         //insert acc1;
         //break;
         }
         }
insert is not done please how can i do this guide me urgent


Best Answer chosen by aswanijagadesh1.397398535585991E12
hitesh90hitesh90
My code is already for bulk of records insert. it will work for data loader..

All Answers

hitesh90hitesh90
Hello,

the mistake is you have written trigger on after Insert. which is wrong for your requirement. 
It should be on before Insert and update.

see below example for your requirement.

Apex Trigger:
trigger trgCheckduplicateAccName on Account (before insert, before update) {
    Map<String, Account> AccountMap = new Map<String, Account>();    
    for (Account acc: System.Trigger.new){    
        if ((acc.Name != null) && (System.Trigger.isInsert || (acc.Name != System.Trigger.oldMap.get(acc.Id).Name))){
            if (AccountMap.containsKey(acc.Name)){        
                acc.Name.addError('Another Account has the '  + 'same Name.');        
            }else{        
                AccountMap.put(acc.Name, acc);       
            }
        }    
    }
    for (Account acc: [SELECT Name FROM Account WHERE Name IN :AccountMap.KeySet()]){  
        Account newAcc = AccountMap.get(acc.Name);        
        newAcc.Name.addError('Account with this Name already exists.');    
    }
}



Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator & Advanced Administrator & Sales cloud consultant
Email :- hiteshpatel.aspl@gmail.com
My Blog:- http://mrjavascript.blogspot.in/
aswanijagadesh1.397398535585991E12aswanijagadesh1.397398535585991E12
how to make this code as a best code?
trigger DuplicateAcc on Account(before insert) {
    List<Account> acc=[select id,name from account];
    public account acc1;
    for (Account acct:Trigger.new) {   
     for(Account a:acc)
     {
       if(acct.name==A.name)
         {          
         acct.addError('ERROR: there is already an identical record Name in Data Base: <a href=\'https://na1.salesforce.com/'  + a.id + '\'+ target="_blank">'+ a.id+'</a>',false);
         return;
         }
     }
     }   
}
i want to mass insert of accounts by using data loader at that time duplicate records will show error  remainling will insert 
hitesh90hitesh90
My code is already for bulk of records insert. it will work for data loader..
This was selected as the best answer
aswanijagadesh1.397398535585991E12aswanijagadesh1.397398535585991E12
thank you hitesh