0

I have written the following class for preventing an Account with the same name to get created in the org. The issue with the trigger is that it is not allowing the record to be saved but the error message to be shown is not as intended.

Apex Trigger:

    trigger AccountTrigger on Account (before insert, before delete) {
        if(Trigger.isBefore){
            if(Trigger.isInsert){
                DuplicateAccount.DuplicateAccountHandler(Trigger.new);
        }
            
    }
}

Apex Class:

public with sharing class DuplicateAccount {
   public static void DuplicateAccountHandler(List<Account> listAccount){
        Set<String> accountName = new Set<String>();
        for(Account accVar : listAccount){
            accountName.add(accVar.Name);
        }
        List<Account> duplicateAccounts = [SELECT Id, Name 
                                             FROM Account 
                                             WHERE Name IN :accountName]; 
        
        for(Account acc : duplicateAccounts){
            if(duplicateAccounts.size()!=0){
                acc.Name.addError('Account cannot be inserted');
            }
        }
    }
}
1

1 Answer 1

1

To add an error to record in a trigger, you must use the records in Trigger.new/Trigger.newMap/Trigger.old/Trigger.oldMap. You're looping over the wrong variable to use for the error.

public with sharing class DuplicateAccount {
   public static void DuplicateAccountHandler(List<Account> listAccount){
        Set<String> accountName = new Set<String>();
        for(Account accVar : listAccount){
            accountName.add(accVar.Name);
        }
        List<Account> duplicateAccounts = [SELECT Id, Name 
                                             FROM Account 
                                             WHERE Name IN :accountName]; 
        
        for(Account acc : listAccount){ // Use Trigger.new records
            if(duplicateAccounts.size()!=0){
                acc.Name.addError('Account cannot be inserted');
            }
        }
    }
}

However, please note that this code will prevent any accounts from being created, as you're also querying the records that were literally just created.

Duplicate Management is relatively difficult to get correct writing your code, so it's strongly suggested you use Duplicate Management instead of writing your code.

There are ways to do this correctly, but you've not thought through all the possible things that could go wrong. If you insist on doing code anyways, you should search here on SFSE for examples that include the proper way to block duplicates in code.

A proper trigger to prevent duplicate must use row locking (FOR UPDATE) to catch all records that may be being updated in other transactions, must check for duplicates in the current batch of this trigger context, must be bulk-safe by checking for each name individually, and must make sure that it doesn't include the records in the current transaction.

1
  • Thanks sfdcfox and David Reed for your inputs. I was aware of the duplicate rules however, the reason for which I wrote the code is just to practice coding in my developer org as I was a full time admin and rarely wrote any code also, I tried creating new records and it was working as expected however, when I was creating a duplicate record the error message was displayed as mentioned in the code along with it the "We hit a snag Please review Account Name field" error was also thrown. Could you please let me know the reason behind this
    – code234
    Commented Apr 12, 2021 at 21:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .