0

I have a concern for below triggers, should we hit governor limits for more than 200 records updated at one go?

Trigger 1:

trigger BatchTrigger on Compliance_Incident_abv__c (after update)
{
if(Trigger.isUpdate )
{
Set<String> activejobstates = new Set<String> {'Queued', 'Processing', 'Preparing'};
List<AsyncApexJob> apexjobs = 
      [Select Id, ApexClass.Name
       from   AsyncApexJob
       where  ApexClass.Name = 'ComplianceIncidents_expireNotify'
       and    Status in :activejobstates];

if(apexjobs.size() == 0)         
{     
for(Compliance_Incident_abv__c s:Trigger.New)
{ if(s.Elapsed_Time_Publish_abv__c==true)
  {
ComplianceIncidents_expireNotify st = new ComplianceIncidents_expireNotify();
database.executeBatch(st);
  }
  }
}

}
}

Trigger 2 :

  trigger Call_Stamper_Abv on Compliance_Incident_abv__c(after update)
  {
  List<Compliance_Incident_abv__c > CIlist = new List<Compliance_Incident_abv__c >();//List Of CIs to update
  List<Compliance_Incident_Transaction_abv__c> TrxnList= new List<Compliance_Incident_Transaction_abv__c> ();//List of CIT's
  Set<Id> callids = new Set<Id>();
  List<Id> callids1 = new List<Id>();
  List<Call2_vod__C> Call2_vod_s= new List<Call2_vod__C> (); //Call2_vod list
  List<Call2_vod__C> Call2_vod_master = new List<Call2_vod__C> ();       //Call2_vod list to update


 Map<Id,Set<Call2_vod__C>> CI_Calls= new Map<Id,Set<Call2_vod__C>>();//mapping of CI & Calls

 if ( trigger.isUpdate ) 
 {
 for ( Compliance_Incident_abv__c acc : trigger.new ) 
 {
 Compliance_Incident_abv__c  accold= trigger.oldMap.get(acc.id);//old record

 if (acc.Status_abv__c=='Open' && acc.Publish_Date_abv__c<>null &&accold.Status_abv__c=='New')
{CIlist.add(acc);///List Of CIs to update
}
}
//TrxnList= [SELECT Account_abv__c, Call_abv__c, Call_Field_abv__c, Call_Detail_abv__c, Call_Sample_abv__c, Compliance_Incident_abv__c, CreatedById, CreatedDate, IsDeleted, Email_Status_abv__c, Email_Status_Detail_abv__c,  Event_Id_abv__c, Event_Location_abv__c, Event_Product_abv__c, Event_Product_Code_abv__c, Event_Product_Desc_abv__c, Event_State_abv__c, Event_Topic_abv__c, Event_Zip_abv__c, From_Address_abv__c, Name, Outcome_abv__c, OutcomeFlag_abv__c, Product_abv__c, Professional_Degree_abv__c, Professional_First_Name_abv__c, Professional_Last_Name_abv__c, Professional_ME_abv__c, Professional_Middle_Name_abv__c, Professional_Salutation_abv__c, Professional_Status_abv__c, Quantity_abv__c, Id, RecordTypeId, Sent_Date_abv__c, Sent_Email_abv__c, Status_abv__c
   //FROM Compliance_Incident_Transaction_abv__c where Compliance_Incident_abv__c in :CIList ];

  if(CIlist.size() > 0)
   {
   //No need to proceed if CIlist = null;

   TrxnList =[select id, Call_abv__c,Compliance_Incident_abv__c FROM Compliance_Incident_Transaction_abv__c where Compliance_Incident_abv__c in :CIList ] ;
  for (Compliance_Incident_Transaction_abv__c  cit: TrxnList)
  {
 //iterate for call id
  callids.add(cit.Call_abv__c);
  }
  System.debug('Call Ids'+ callids);// SET OF CALL IDs


Call2_vod_s=[SELECT id, Name,Account_vod__c, Incident_Exists_Flag_abv__c, Is_Parent_Call_vod__c, Parent_Call_vod__c 
  FROM Call2_vod__c where id in :callids];
//List of corr. calls, whose id are in callids
}// Null chk    
 for(Call2_vod__c  c: Call2_vod_s)
{
Call2_vod_master.add(c);//Add all child calls to master List

if(c.Parent_Call_vod__c!= null)
{//IF PArent CALL exists for given, Add into 'master list', ignore if null

 callids1.add(c.Parent_Call_vod__c);// Parent_Call_vod__c  contains ID

 }
else continue;

 }

Call2_vod_s.clear();// clear the call list of children now

Call2_vod_s=[SELECT id, Name,Account_vod__c, Incident_Exists_Flag_abv__c, Is_Parent_Call_vod__c, Parent_Call_vod__c 
FROM Call2_vod__c where id in :callids1];

 for(Call2_vod__c  c: Call2_vod_s)
{
Call2_vod_master.add(c);// Add PARENTS
}

for(Call2_vod__c  c: Call2_vod_master)
{
c.Override_lock_vod__c=true;
c.Incident_Exists_Flag_abv__c =true ;// STAMP IT

}

try
{
update Call2_vod_master; //DML
}
 catch(DMLException x)
 {
 System.debug(' DML Ex'+ x.getMessage());
 }

}
}
3
  • @Salesforce Learner. Can you please help me here? Atleast for first trigger – Testing_SFDC Sep 10 '15 at 16:01
  • Do you hit the governor limits on both the triggers? – Salesforce Learner Sep 10 '15 at 17:17
  • 2
    SOQL inside loops is common problem but because your code is not appropriately indented such problems are harder to see. – Keith C Sep 10 '15 at 18:03
1

As well as doing code reviews to look for problems, if you are concerned about how an area of code handles the bulk case add an extra test that checks the bulk case. That way you can check that the code works today and will be protected in the future if changes are made that cause the code to fail for the bulk case.

Based on a quick look, I don't see any obvious bulk problems in trigger 2.

But trigger 1 looks like it contains this anti-pattern: Is using Database.executeBatch from a trigger an anti-pattern? that is likely to cause the:

Maximum number of Batch Apex jobs queued or active concurrently

governor limit of 5 to be exceeded in the bulk case.

Not the answer you're looking for? Browse other questions tagged or ask your own question.