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
AKallAKall 

Governor Limit Problem

We are experiencing a problem with governor limits where a trigger we have created results in the following error.
 
Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger OpportunityCampaignUpdate caused an unexpected exception, contact your administrator: OpportunityCampaignUpdate: execution of AfterUpdate caused by: System.Exception: Too many query rows: 1001: Trigger.OpportunityCampaignUpdate: line 17, column 36".
 
There is really no way that this trigger could be acting on more than two records at a time, and it is absolutely impossible that it would be acting on more than 50 records at a time. The error occurs when we invoke the trigger from only one record. So we are curious to know if there is an internal trigger on the campaign object that is causing the error.
Code:
trigger OpportunityCampaignUpdate on Opportunity (after insert, after update) {
 
 /* Synchronizes opportunity approval with Campaign member response history */

 MAP<ID,LIST<ID>> cmids = new MAP<ID,LIST<ID>>{};
 for(Opportunity o: Trigger.New){
  if(o.WebTrialApproved__c == true){
   if (cmids.containskey(o.CampaignId)){
    cmids.get(o.CampaignId).add(o.Contact__r.Id);
   } else {
    cmids.put(o.CampaignId,new LIST<ID>{o.Contact__r.Id});
   }
  }
 }
 if (cmids.size()>0){
  for (ID c: cmids.keyset()){ 
   ID[] ccmids = cmids.get(c);
   CampaignMember[] cms = [Select ID, Status From CampaignMember Where (ContactID IN :ccmids OR Lead.ConvertedContactID IN :ccmids) AND CampaignId = :c];
   for(CampaignMember cm:cms){
    cm.Status = 'Trial Activated';
   }
   update cms;
  }
 }

}