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
Nageswara  reddyNageswara reddy 

Urgent : Trigger error

HI all,

 

       I am   moving  one trigger from   sandbox account  to production. in  production account i have 34,200 contacts are there.  i ma not getting  any error while uploading  . but  I am  getting failure message  in  production  while  validating the chage set. The Fialure message like ""System.LimitException: Too many query rows: 50001", Failure Stack Trace: "(System Code) Trigger.campMemberTrig: line 6, column 1" as my trigger is 

 

trigger campMemberTrig on CampaignMember (before insert, before update) {

Map<Id, Contact> mpCon = new Map<Id, Contact>();
list<contact> conlist=new list<contact>();
for(Contact c: [Select id, name,email,HasOptedOutOfEmail from Contact limti 50000])---------- 6th line   I tried  49000 limit a but //no Use same error iam getting once  
mpcon.put(c.id,c);
id contactid;
for(CampaignMember cm:trigger.New) {
if(cm.status=='Opted Out'&&trigger.isinsert)
contactid=cm.contactid;
else if(trigger.isupdate)
if(trigger.oldmap.get(cm.id).status!='Opted Out'&&cm.status=='Opted Out')
contactid=cm.contactid;
}


If(contactid!=null){
string cemail=mpcon.get(contactid).email;
for(id cid:mpcon.keyset())
{
If(mpcon.get(cid).email==cemail){
mpcon.get(cid).HasOptedOutOfEmail=true;
conlist.add(mpcon.get(cid));
}
}
}
if(conlist.size()>0)
update conlist;

}

 

 

Plz help me out this situation

 

Nageswara 

 

HariDineshHariDinesh

Hi,


You know that Total number of records retrieved by SOQL queries is 50,000.

 

Here I would like to specify one point
The total number of SOQL queries means not by a single Query.
It will consider the total number of rows Retrieved by that Particular instance.


In your case the code which caused to fire this Trigger already queried some records
So even though if you are trying to query only 34,200 this count will be added to that already queried records count.

So that total count is reaching more than 50,000

 

In your case

 

Records Queried by code causing this trigger fire+ query in trigger >50,000.

 

Because of this reason you are getting this error.

To solve this problem you need to Filter that queries by using where condition (better use Index Fields)
So that the number of queries returned will be decreased and you will get rid of this problem.