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
SunchaserSunchaser 

Trigger not working!

Hi,

 

I am new with Apex and, I was wondering if anyone can tell me what is wrong with the following code:

 

I was trying to compare the zip codes in the myset set with the BillingPostalCode of the account records.  Even though the code didn't give any errors, when I run it,  I get "maximum trigger depth exceeded" error. I guess  there is a problem with the loop and it is trying to run the same line over an over. 

 

 

trigger testtrigger on Account (After Insert, after update) {

Set<String> myset = new Set<String>{'87543','08502', '07541'}; 



String s = 'Tester'; 


for (Account accs : [select id, name,BillingPostalCode from account where BillingPostalCode in :myset]){ 
  


accs.Name = 'test successful';

update accs;

       }
    
}

 

Help will be  really appreciated.

Thanks!

 

Best Answer chosen by Admin (Salesforce Developers) 
tmatthiesentmatthiesen

Here you go:

 

 

trigger testtrigger on Account (Before insert, before update) {
//populate set
Set<String> myset = new Set<String>{'87543','08502', '07541'}; 
//iterate through Trigger.new
    for(Account A : Trigger.new){
//Check if Account field is in myset
        if(myset.contains(A.BillingPostalCode)){
/*if true, set value - notice you can set the val directly, you don't need to requery since you already have a handle to the record(s)
*/
            A.Name = 'test successful';
        }  
    }   
} 

 

 

All Answers

pearlbearpearlbear

I'm pretty new to APEX in general, but the basic tip is to not run any SOQL or DML actions within a for loop. I can't quite tell what you are trying to do with this code, but try not to do that.

_Prasu__Prasu_

This is due to the recursive call.

 

Your "update accs;" statement again firing the same trigger which making a never ending loop.

 

Please modify your trigger to before insert,before update.

Converting it to before trigger will also save one DML call, you will not need to update those accounts again.

 

 

tmatthiesentmatthiesen

Here you go:

 

 

trigger testtrigger on Account (Before insert, before update) {
//populate set
Set<String> myset = new Set<String>{'87543','08502', '07541'}; 
//iterate through Trigger.new
    for(Account A : Trigger.new){
//Check if Account field is in myset
        if(myset.contains(A.BillingPostalCode)){
/*if true, set value - notice you can set the val directly, you don't need to requery since you already have a handle to the record(s)
*/
            A.Name = 'test successful';
        }  
    }   
} 

 

 

This was selected as the best answer
SunchaserSunchaser

Great! Works  like a charm, thanks for your help (and also for the comments)  tmatthiesen!

 

 

SunchaserSunchaser

Thanks for the command pearlbear. Actually I found a similar for loop with SOQL on one of the apex tutorials , I just modified it slightly. But I'll keep that in mind!

SunchaserSunchaser

Thanks for your post eprasu!  before insert,before update didn't solve the problem but good to know about DML call.