0
votes

I am trying to write a Trigger for before insert event on Account object.

  trigger DeduplicationAccount on Account (before insert) {        
       //Get all the accounts in action in 'insert'
       Account[] inputAccountList = Trigger.NEW;

I am trying to get a relative list of accounts in my input list of accounts. Say for eample, I am trying to get such accounts where the last name = 'XXX' in my trigger new.

So, i am writing like this: // Here, listOfSurname is containing a list of surname with 'XXX'

for(Account ac: Trigger.new){
       List<Account> accountDuplicate = [Select ac.rr_First_Name__c, ac.rr_Last_Name__c  From 
       Account ac where ac.rr_Last_Name__c IN : listOfSurname];
       System.debug('accountDuplicate: '+ accountDuplicate);
       }

But, this list is always coming as 0 though im my input,an account have surname as 'XXX'.

1

1 Answers

0
votes

Trigger.New has all the information for the record and we could use that to verify against any condition. I rephrased your query below to check if each account's lastname in Trigger.new is part of the list.

for(Account ac: Trigger.new){
   for(String s: listOfSurname){
      if (ac.rr_Last_Name__c == s){
         System.debug('accountDuplicate: '+ ac);
         break;
      }
   }
}