You need to sign in to do that
Don't have an account?
Lakshman
Bulkified trigger still throwing exception
Hi All,
I have a trigger as follows:
trigger TriggerOnCase on Case (before update) { Set<Id> caseIds = new Set<Id>(); List<PermissionSetAssignment> visitorPerm = new List<PermissionSetAssignment>();
visitorPerm = [SELECT PermissionSetId FROM PermissionSetAssignment WHERE AssigneeId= :UserInfo.getUserId() AND PermissionSet.Name = 'Visitor']; if(visitorPerm .size()>0) { for(Case c : trigger.new) { //Case status is closed if(c.Status == 'Closed') { caseIds.add(c.Id); //a list of cases } } CaseReply__c[] crcases = [select rCase__c,Id FROM CustObject__c WHERE tatus__c = 0.0 And rCase__r.Id in :caseIds]; for (Case c: trigger.new) { for(CustomObject__c cr : custCases) { if(c.Id == cr.rCase__c) { c.Status.addError('Please chk data!!!!'); } } } } }
The above trigger looks bulkified to me but I still get an error like:- Failure Message: "System.LimitException: Too many SOQL queries: 101", Failure Stack Trace: "Trigger.CaseBeforeUpdate: line 4, column 1", when I try to deploy code into prod with test classes having an Update operation Case.
Can any one please help me get out of this.
Thanks in advance.
Regards,
Lakshman
Hi,
I don’t find anything wrong in your trigger. Try one thing i.e. Not create an instance of the list instead of that please make the query directly like below:
List<PermissionSetAssignment> visitorPerm = [SELECT PermissionSetId FROM PermissionSetAssignment WHERE AssigneeId= :UserInfo.getUserId() AND PermissionSet.Name = 'Visitor'];
How will this help? The query will still ge executed. When we deploy to prod all the test classes get executed. I am guessing that tere are many test classes which have case update operation so this error is coming, what are your thoughts on this?
Regards,
Lakshman
Do you have any other triggers on Case?
Yes, there are other triggers on Case like after insert, after update, before insert. All seperate triggers.
Regards,
Lakshman
Do you have any triggers on CaseComment?
No, only on Case and on Task
Do you have any tests which do this:
update case1;
update case1;
update case1;
update case1;
etc?
yes I have many such test classes, I also think that it is causing the issue......how do I avoid it?
It depends on the tests and what you are doing / testing.
One way is this:
Case1 = new Case();
Case2 = new Case();
Case3 = new Case();
list<Case> testList = new list<Case>();
testList.add(Case1);
testList.add(Case2);
testList.add(Case3);
insert testList;
Case1.field = 'new';
Case2.field = 'new'
Case3.field = 'new';
update testList;
But there are many test classes behaving diferently.....
You have a limit on SOQL queries per Test.
Your code could be fine but the number of SOQL queries in your test is throwing the exception.
this is really strange, when I test in fulll sbox its fine but in prod it goes weird....
Are you using
@isTest(SeeAllData=true)
No I am not using that.
Do you have some apex code on version 23 and some on version 24?
Yes, I have that.
So there was a change from 23 to 24.
Tests in 24 now exclude data from the org unless you use the annotation @isTest(SeeAllData=true)
So even though a 23 based test executes in Sandbox does not mean it will execute in production when it has to deal with all your actual data.