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
LakshmanLakshman 

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

 

 

Navatar_DbSupNavatar_DbSup

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'];

LakshmanLakshman

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

 

 

carlocarlo

Do you have any other triggers on Case?

LakshmanLakshman

Yes, there are other triggers on Case like after insert, after update, before insert. All seperate triggers.

 

Regards,

Lakshman

carlocarlo

Do you have any triggers on CaseComment?

LakshmanLakshman

No, only on Case and on Task

carlocarlo

Do you have any tests which do this:

 

update case1;

update case1;

update case1;

update case1;

 

etc?

LakshmanLakshman

yes I have many such test classes, I also think that it is causing the issue......how do I avoid it?

carlocarlo

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;

 

 

LakshmanLakshman

But there are many test classes behaving diferently.....

carlocarlo

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.

LakshmanLakshman

this is really strange, when I test in fulll sbox its fine but in prod it goes weird....

carlocarlo

Are you using 

 

@isTest(SeeAllData=true)     

LakshmanLakshman

No I am not using that.

carlocarlo

Do you have some apex code on version 23 and some on version 24?

LakshmanLakshman

Yes, I have that.

carlocarlo

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.