You need to sign in to do that
Don't have an account?
Sunchaser
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!
Here you go:
All Answers
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.
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.
Here you go:
Great! Works like a charm, thanks for your help (and also for the comments) tmatthiesen!
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!
Thanks for your post eprasu! before insert,before update didn't solve the problem but good to know about DML call.