+ Start a Discussion
alok29novalok29nov 

Duplicate account can not be entered with same parent id

Hi All,

 

I have written a query which prevents from entring a duplicte account. I just want to add one more condition.

If parent account of the account being entered is same only then I want make sure that account is not the duplicate one. And if parent id is different then an account with same name can be entered.

 

My code is below:

 

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

Map<String, Account> accMap = 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 (accMap.containsKey(acc.Name)) {

  acc.Name.addError('Another new acconut has the ' + 'same email address.');

} else {

    accMap.put(acc.Name, acc); } }

}

for (Account acc : [SELECT Name FROM Account WHERE Name IN :accMap.KeySet()]) {     Account newAccount = accMap.get(acc.Name);     newAccount.Name.addError('A account with this name' + '  already exists.');

}}

 

 

How the above code can be modified to accomodate the above condition.

 

Thanks in advance!

 

~Alok