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
sales@myvarmasales@myvarma 

triggers on account records

instead error 
i must update the new values to old record with same name help me



trigger accounttrigger on Account (before insert, before update) {

     Map<String, Id> mapAccount = new Map<String, Id>();
    
    Set<String> setAccName = new Set<String>();
    for(Account acc : trigger.new)
        setAccName.add(acc.Name);
        
    for(Account acc : [ SELECT Id, Name FROM   Account WHERE  Name IN :setAccName ] )
        mapAccount.put(acc.Name, acc.Id);
    
    for(Account acc : trigger.new)
        if(mapAccount.containsKey(acc.Name) && mapAccount.get(acc.Name) != acc.Id)
            acc.addError( 'There is already another Account with the same Name. '  +  
                mapAccount.get(acc.Name) + '\'>' + acc.Name + '</a>', FALSE );
}
kirubakaran viswanathankirubakaran viswanathan
Hi,

You cannot get the acc.id for Before Trigger. So instead of checking != acc.id use
if(mapAccount.containsKey(acc.Name))
            acc.addError( 'There is already another Account with the same Name. '  +  
                mapAccount.get(acc.Name) + '\'>' + acc.Name + '</a>', FALSE );

This will write up the error message if you try to create or update the Account with existing name.

Please mark this answer as best answer if it resolves your problem
sales@myvarmasales@myvarma
should i write any class for this trigger?
sales@myvarmasales@myvarma
its not working bro