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
Rajendra Prasad 44Rajendra Prasad 44 

i need to show error message when duplicate account record is created based on account Name

trigger acctrigger on Account (before insert) {
    if(trigger.isBefore){    
      if(trigger.isInsert){
      list<Account> acc = [SELECT Id,Name from Account];
      for(Account ta: trigger.new){
          for(Account a: acc){
             if(ta.Name == a.Name){
               newval.addError('sorry you cannot add this account its already been in database');
           }
         }
       }
     }
   }
 }
Best Answer chosen by Rajendra Prasad 44
Rajendra Prasad 44Rajendra Prasad 44
Thanks Prakhar, i think using map also best practice.

t
trigger manageAccountDuplicates on Account (before insert, before update){
List<String> uniqueValues = new List<String>();
    for(Account record : Trigger.new){
        uniqueValueList.add(record.Unique_Value__c);
    }
Map<String,Account> uniqueValueMap = new Map<String,Account>();
    for(Account record : [
        SELECT Unique_Value__c FROM Account
        WHERE Unique_Value__c IN :uniqueValueList
    ]){
        uniqueValueMap.put(record.Unique_Value__c, record);        
    }
for(Account record : Trigger.new){
        if(uniqueValueMap.containsKey(record.Unique_Value__c)){
            if(trigger.isInsert || (trigger.isUpdate && record.id<>uniqueValueMap.get(record.Unique_Value__c).id)){
                record.addError('An account matching this criteria already exists');
            }           
        }
    }
}


 

All Answers

Prakhar Saxena 19Prakhar Saxena 19
Hi Rajendra,

Avoid using nested for loops. I tried this and it works fine:
 
trigger AddErrorOnDuplicateAccount on Account (before insert) {

    if(trigger.isBefore && trigger.isInsert){
        List<Account> listOfAccounts = [SELECT Name FROM Account];
        List<String> listOfAccountNames = new List<String>();
        for(Account a : listOfAccounts){
            listOfAccountNames.add(a.Name);
        }
        
        for(Account a : trigger.new){
            if(listOfAccountNames.contains(a.Name)){
                a.addError('Sorry you cannot add this account, its already in database');
            }
        }
    }
}

 
Rajendra Prasad 44Rajendra Prasad 44
Thanks Prakhar, i think using map also best practice.

t
trigger manageAccountDuplicates on Account (before insert, before update){
List<String> uniqueValues = new List<String>();
    for(Account record : Trigger.new){
        uniqueValueList.add(record.Unique_Value__c);
    }
Map<String,Account> uniqueValueMap = new Map<String,Account>();
    for(Account record : [
        SELECT Unique_Value__c FROM Account
        WHERE Unique_Value__c IN :uniqueValueList
    ]){
        uniqueValueMap.put(record.Unique_Value__c, record);        
    }
for(Account record : Trigger.new){
        if(uniqueValueMap.containsKey(record.Unique_Value__c)){
            if(trigger.isInsert || (trigger.isUpdate && record.id<>uniqueValueMap.get(record.Unique_Value__c).id)){
                record.addError('An account matching this criteria already exists');
            }           
        }
    }
}


 
This was selected as the best answer