You need to sign in to do that
Don't have an account?

URGENT HELP on trigger
I have an object Flag_Reporter__c which is child object in master detail relationship with Account...in an account trigger I have set the following code..in test class I having error: System.LimitException: Too many SOQL queries: 101
trigger Trg_After_Flag_Updater on Account (after update) { Boolean updateFlag = false; Boolean insertFlag = false; List<Flag_Reporter__c> lstFlagReporter = new List<Flag_Reporter__c>(); Set<ID> aID = trigger.newMap.keySet(); Map<ID,Flag_Reporter__c> fRC= New Map<ID,Flag_Reporter__c>(); List<Flag_Reporter__c> flags = [Select Overall_Status__c,Overall_Status_Ranking__c,Account_Flag__r.Id,Flag_for_Editorial__c from Flag_Reporter__c Where Account_Flag__c IN : aID]; for(Flag_Reporter__c fr: flags){ fRC.put(fr.Account_Flag__r.Id,fr); } for(Account accFlag:Trigger.new){ Flag_Reporter__c fr; String editorial; if(fRC.get(accFlag.Id)!=NULL) fr = fRC.get(accFlag.Id); if(fr!=NULL){ updateFlag = true; if(fr.Overall_Status__c == 'RED') fr.Overall_Status_Ranking__c = 1; else if(fr.Overall_Status__c == 'YELLOW') fr.Overall_Status_Ranking__c = 6; else if(fr.Overall_Status__c == 'GREEN') fr.Overall_Status_Ranking__c = 8; if(accFlag.Partner_Reviews__c!=NULL){ if(accFlag.Partner_Reviews__c == '1') fr.Flag_for_Editorial__c= 'RED'; else fr.Flag_for_Editorial__c= 'GREEN'; } lstFlagReporter.add(fr); }if(fr == NULL){ insertFlag=true; lstFlagReporter.add(new Flag_Reporter__c(Account_Flag__c=accFlag.Id)); } }try{ if(updateFlag==true) update lstFlagReporter; if(insertFlag==true) insert lstFlagReporter; }catch(Exception ex){ ex.getMessage(); } }
stack is showing error in this place:
List<Flag_Reporter__c> flags = [Select Overall_Status__c,Overall_Status_Ranking__c,Account_Flag__r.Id,Flag_for_Editorial__c from Flag_Reporter__c Where Account_Flag__c IN : aID];
Hi,
Trigger looks fine to me.
Is there any trigger on your custom object "Flag_Reporter__c" which is updating the "Account"? If yes, then the two trigger are calling each other in recursion. (In that case you can find out how to avoid recursion while writing triggers on discusstion forum)
Else, you can write a system.debug('Trigger Called::::::::') statement on your trigger code or you can use "Limits" class to find out where the query limit is crossing in current context.
Here's a link - http://wiki.developerforce.com/page/Best_Practice:_Use_of_the_Limits_Apex_Methods_to_avoid_Hitting_Governor_Limits
Its hard to understand from the code where the exact issue is :(. But for sure you are crossing governors..
All Answers
Hi,
Not sure if you have used "Test.startTest()" and "Test.stopTest()" in your test method. (try and check if problem persists)
Also if possible can u paste your test class here.. so that it will be easy to provide you solution.
Here is the code now I am getting a different error:
error is now:
System.FinalException: Testing already started
pointing at
if(fr!=NULL){
problem is still persisting :(
Hi,
Not sure if you are testing a VF Page or a Trigger.. anyways..
Just remove all "test.StartTest()" and test.StopTest()" statements from the Test class.
Place one Test.startTest() and the beginning (just after you start writing the test method) and one in the end.
Also it is always better to write your test methods seperately for a VF Page and for a trigger as that will help you reduce no. of DML operations per context.
Hi,
You can have only one "StartTest" and "StopTest" in your entire test method ;)
changed it but still getting the same error...this test class is for a class ..during execution it is calling that trigger... so getting the same problem..here is the renewed test class
Here is the save method..for which I am getting the problem...
Hi,
Just place your StartTest() statement before the insertion of Account.
Also check if Test class for Account trigger has "StartTest" and "StopTest".
Also from your code it looks like that there are no DML oprations or SOQL statements in your Save() method.
Just make sure that in your code/test class you have not written any queries in For loop.
It has an update at the last in Save method please check...
one update is fine,
but somewhere in your context other code snippet is getting called.. So number of SOQL for the context is crossing the governors of 100. StartTest and StopTest limits the governors context to that block only..
Save method can only that trigger that I pasted..do you think that trigger is calling something in loop please have a look and tell me
Hi,
Trigger looks fine to me.
Is there any trigger on your custom object "Flag_Reporter__c" which is updating the "Account"? If yes, then the two trigger are calling each other in recursion. (In that case you can find out how to avoid recursion while writing triggers on discusstion forum)
Else, you can write a system.debug('Trigger Called::::::::') statement on your trigger code or you can use "Limits" class to find out where the query limit is crossing in current context.
Here's a link - http://wiki.developerforce.com/page/Best_Practice:_Use_of_the_Limits_Apex_Methods_to_avoid_Hitting_Governor_Limits
Its hard to understand from the code where the exact issue is :(. But for sure you are crossing governors..
Hi,
There's an work flow which updates a field in Account due to update in Flag Reporter...
Ok.
So the issue is related with the calling the same trigger recursively.
One workaround is to create your acount and update it in such a way that it will not fire workflow (try to set the criteria in u r code in such a way that workflow should not fire.) If that is not possible, try to find on forum about avoiding recursive triggers.
Going to bed now :) . If possible i will be online tomorrow.. and we can try to resolve this issue. Hope before that you will be able to resolve this one ;) .
If you need any help just drop me an email to mandy.kool@yahoo.com; i will try to help.
Have a gr8 day!!!
Thanks Kulkarni..solved it..it was related with the cycle of updation..needed to break it..