1

In the below code, line 31(AggregateResult results = database.query(str)), I have used Database.query inside a for loop. If this causes governor limit issue in future, please help me in optimizing this.

global database.QueryLocator start(Database.BatchableContext BC){

   return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List<sObject> scope){
    List<SummaryData__c> summaryDataToUpdate = new List<SummaryData__c>();   
    Map<Id, String> newMap = new Map<Id, String>();
    Map<Id, Decimal> mapCals = new Map<Id, Decimal>();
    List<summaryData__c> toUpdate = new List<summaryData__C>();
    for(SummaryData__c summaryData : (List<SummaryData__c>)scope)
    {

        String queryString = 'SELECT count() FROM ' + summaryData.ObjectName__c + ' WHERE ' + summaryData.WhereClause__c;
        newMap.put(summaryData.id,queryString);

    }
    for(Id id: newMap.keySet())
    {
      for(String str: newMap.Values() )
      {
        if(newmap.get(id) == str)
        {
          AggregateResult results = database.query(str);
          Decimal recordCount = (Decimal)results.get('result');
          mapCals.put(id,recordCount);
        }
      }
    }
    for(Id id: mapCals.keySet())
    {
      summaryData__c s = new summaryData__c();
      s.id = id;
      s.RecordCount__c = mapCals.get(id);
      s.LastRunTime__c = DateTime.Now();
      toUpdate.add(s);

    }
    if(!toUpdate.Isempty())
    {
      Update toUpdate;
    }

}
global void finish(Database.BatchableContext BC){
    Messaging.SingleEmailMessage sm = new Messaging.SingleEmailMessage();
    String[] sendingTo = new String[]{'dburgess@sdimktg.com','sshah@sdimktg.com'};
    sm.setToAddresses(sendingTo);
    sm.setSubject('Record Count Job Execution');
    sm.setPlainTextBody('Job was executed');
    Messaging.sendEmail(new Messaging.SingleEmailMessage[]{sm});
}
  • I would be extremely wary of implementing an object to define the where clause and object name for a query in this way. You should probably just set the batch size to one with Database.executeBatch(batchInstance, scopeSize). – Adrian Larson Sep 26 '16 at 18:24
  • So setting Database.executeBatch(batchInstance, 1) will avoid hitting any limits? Also, can you advise me if I can optimize this in anyway instead of setting batch size as 1 – SFDC_BigDog Sep 26 '16 at 18:28
  • 1
    Given that you are trying to perform a query for each record in the scope, it is extremely unwise to set any other batch size. – Adrian Larson Sep 26 '16 at 18:29
  • Thanks for that. But can I modify my code in anyway? – SFDC_BigDog Sep 26 '16 at 18:30
1

Given that you are trying to perform one query for each record in your scope, it is extremely unwise to even attempt executing this batch for any scope size other than 1. I do not think you will be able to restructure your code to get around that critical bottleneck.

Database.executeBatch(new MyBatch(), /*scopeSize*/ 1);
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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