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
Manish Kumar 23Manish Kumar 23 

Trigger for Duplicate check account

Hi All,

I have written a trigger to check the duplicate name of account:

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

    Map<String, Account> accountMap = new Map<String, Account>();
    for (Account account : System.Trigger.new) {
       
        // Make sure we don't treat an Name that 
        // isn't changing during an update as a duplicate. 
   
        if ((account.Name != null) &&
                (System.Trigger.isInsert ||
                (account.Name !=
                    System.Trigger.oldMap.get(account.Id).Name))) {
       
            // Make sure another new account isn't also a duplicate 
   
            if (accountMap.containsKey(account.Name)) {
                account.Name.addError('Another new account has the '
                                    + 'same Name.');
            } else {
                accountMap.put(account.Name, account);
            }
       }
    }
   
    // Using a single database query, find all the accounts in 
   
    // the database that have the same Name  as any 
   
    // of the accounts being inserted or updated. 
   
    for (Account account : [SELECT Name FROM Account
                      WHERE Name IN :accountMap.KeySet()]) {
        Account newAccount = accountMap.get(account.Name);
        newAccount.Name.addError('A account with this Name '
                               + 'already exists.');
    }
}



now problem is I am getting"Developer script exception from Gmail : accountDuplicatePreventer : accountDuplicatePreventer: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.accountDuplicatePreventer: line 35, column 1"

I already have account name-"Manish compnay"
Now I want to insert  another account name-"manish company"

In this case I am geeting above error.
Request your help.
Virendra ChouhanVirendra Chouhan
Hello Manish,

why you use this code---
for (Account account : [SELECT Name FROM Account
                      WHERE Name IN :accountMap.KeySet()]) {
        Account newAccount = accountMap.get(account.Name);
        newAccount.Name.addError('A account with this Name '
                               + 'already exists.');
    }

While your work is done through the above code.
Vatsal KothariVatsal Kothari
Hi Manish,

You can refer below updated code:
trigger PreventDuplicateAccount on Account (before insert,before update) 
{	
	Map<String, Account> accNameMap = new Map<String, Account>();
	
	for(Account acc: [Select Id,Name from Account]){
		if(!accNameMap.containsKey(acc.Name)){ 
			accNameMap.put(acc.Name,acc); 
		}			
	}

	if(trigger.isInsert){	
		for (Account acc : Trigger.new){
			if(accNameMap.containsKey(acc.Name)){
				acc.Name.addError('Account with this Name already exists');
			}
		}                        
	}
	
	if(Trigger.isUpdate){
		for(Account acc : Trigger.new){
			if(acc.Name != Trigger.oldMap.get(acc.Id).Name && accNameMap.containsKey(acc.Name)){
				acc.Name.addError('Account with this Name already exists');
			}
		}
	}
}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal
Manish Kumar 23Manish Kumar 23
Thank you vishal, Whether this code will also check the Case sensitivity.i.e it will treat MANISH and Manish as same.
Vatsal KothariVatsal Kothari
It will treat MANISH and Manish as different.
Manish Kumar 23Manish Kumar 23
thanx,

Is there any that It should treat one.
Vatsal KothariVatsal Kothari
Below is the updated code:
trigger PreventDuplicateAccount on Account (before insert,before update) 
{   
    Map<String, Account> accNameMap = new Map<String, Account>();
    
    for(Account acc: [Select Id,Name from Account]){
        String accName = acc.Name;
        accName = accName.touppercase();
        if(!accNameMap.containsKey(accName)){ 
            accNameMap.put(accName,acc); 
        }           
    }

    if(trigger.isInsert){   
        for (Account acc : Trigger.new){
            String accName = acc.Name;
            accName = accName.touppercase();
            if(accNameMap.containsKey(accName)){
                acc.Name.addError('Account with this Name already exists');
            }
        }                        
    }
    
    if(Trigger.isUpdate){
        for(Account acc : Trigger.new){
            String newAccName = acc.Name;
            String oldAccName = Trigger.oldMap.get(acc.Id).Name;
            newAccName = newAccName.touppercase();
            oldAccName = oldAccName.touppercase();
            if(oldAccName != newAccName && accNameMap.containsKey(newAccName)){
                acc.Name.addError('Account with this Name already exists');
            }
        }
    }
}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal