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
rajesh k 10rajesh k 10 

How to check duplicate names account update(one error mesg) and insert (another error mesg)in account using trigger?

 
Shaijan ThomasShaijan Thomas
Can you give more detail on this. Using addError will solve your Issue.
Shaijan
rajesh k 10rajesh k 10
Hi,

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:
> “There is another account in the system with this exact name and is a likely duplicate."

- 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’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"

help me..
Shaijan ThomasShaijan Thomas
1. Get all names from Trigger.New (ie string list)
2. Query in Account with that names ie [ select Name from account where id in : TriggerNewList
3. Get the names from the list in new string list (2ndNameList)
4. Run a for loop on Trigger.New, inside again a for with 2ndNameList, compage Name from trigger and 2nd name liast and adderror to trgger .New
Thanks
Shaijan
rajesh k 10rajesh k 10
Hi,
        I tried like below this is currect or not

trigger AccountDuplicateTrg 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))
           if(Trigger.isInsert){
             acc1.addError('There is another account in the system with this exact name and is a likely duplicate.');
           }
           else if(Trigger.isUpdate){
               acc1.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');
           
           }
        }
  }

updation time this currect or not?
Shaijan ThomasShaijan Thomas
looks good, just try and let me know
Thanks
Shaijan
SunidharSunidhar
Try this once, think this will help for you, if you want to compare the account name with all the account names then u nned to query all the records in database

trigger AccountDuplicateTrg on Account (before insert,before update) {
    
    map<String,Account> acc_map = new map<String, Account>();
    
    for(Account a: [SELECT id,name FROM Account]) {
        acc_map.put(a.name,a);
    }
    
    for(Account acc: trigger.new) {
        if(acc_map.containsKey(acc.name)) {
            if(trigger.isInsert)
                acc.addError('There is another account in the system with this exact name and is a likely duplicate.');
            else if(trigger.isUpdate) {
                acc.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');
            }
        }
    }
}