5

This is the trigger i have wrote to prevent the creation of duplicate accounts, i am getting an error message saying "Intial term of field expression must be a concrete SObject:LIST ".

Can someone help me in scrubbing this code, so that it might work. thanks.

trigger AccountDuplicateTrigger on Account (before insert,before update) {
    for(Account a:Trigger.new)
    {
        List<Account> acc=[select ID from account where Name=:a.Name and Rating=:a.rating];
        if(acc.size()>0)
        {
            acc.name.adderror('You cannot create a dulplicate account');
        }
    }
}
1
  • 3
    You might hit the governor limit by having a SOQL query within a for loop – Prady Dec 8 '13 at 5:40
8

@Shebin answered your specific question, but there are a few issue with your trigger, the most important being that it is not bulkified. you always want to avoid SOQL inside loops.

Something like below is a bit safer to use

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

     //You may need to filter this a bit more if you have a very large number of accounts    
     map<Id,Account> existingAccountMap = new  map<Id,Account>([Select Id, Name, Rating From Account Where Rating != null]); 

     for(Account a : Trigger.new){
        //Remember to use Double equal
        if(a.name == existingAccountMap.get(a.Id).Name && a.rating == existingAccountMap.get(a.Id).rating){
          a.adderror('You cannot create a dulplicate account');
        }
     }
}
1
  • 1
    Will it work on before insert , it will give NPE error. the above solution will work on the before update i think that to if we run loop through the trigger.old. – Umesh Beti Jun 12 '19 at 11:41
1

Instead of acc.name for acc.name.addError use a.adderror As adderror is for an sobject so you need to use a instead of acc and also acc.name is not valid as acc is a list. Below is the corrected code

 trigger AccountDuplicateTrigger on Account (before insert,before update) {
        for(Account a:Trigger.new)
        {
            List<Account> acc=[select ID from account where Name=:a.Name and Rating=:a.rating];
            if(acc.size()>0)
            {
                a.adderror('You cannot create a dulplicate account');
            }
        }
    }
0
0
trigger AccountDuplicateCheckTrigger on Account (before insert) {


       //Preparing Account names in Set from trigger.new

        Set<String> nameSet = new Set<String>();

        for(Account acc : trigger.new){

            nameSet.add(acc.name);        

        }

        // getting the list of accounts in database  with the account name we entered ( trigger.new)

        List<Account> accList = new List<Account>(
            [select id,name from Account where name in: nameSet]);

        for(Account a : trigger.new){

            if(accList.size() > 0 )
                a.addError('Account already exists in your Organization with name '+a.name);

        }

}
1
  • 3
    Simply posting code does not really help the community or the OP as there is no explanation of what the problem was or your proposed solution. Please add some clarifying information around your answer. – Eric Jul 6 '15 at 16:46
0

An easier way to solve the problem might be to implement the free DupeCatcher app. It works pretty well at forcing users to search for duplicates before they create new records.

Here's a blog post that explains how to use it and where to find DupeCatcher. enter link description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.