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

Batch Apex Issue



I have created a batch apex class which assigns leads , opps, accounts to appropriate territories as per territory rules and other business logic.


I am clicking on custom button 'Run Rules' in Territory List View and calling the batch apex class and related method using javascript behaviour.


Right now, I am just testing for Leads(there are nearly 25K leads). 118 batches run properly while 1 batch fails. The error that it gives as per Debug log is:-


20091021124930.554:External entry point:     returning NULL from method global Database.QueryLocator start(Database.BatchableContext) in 0 ms

 This is my snapshot of Apex Jobs processing:-


ActionSubmitted DateSorted DescendingJob TypeStatusTotal BatchesBatches ProcessedFailuresSubmitted ByCompletion DateApex ClassApex Method
 10/21/2009 7:49 AMBatch ApexFailed011Desai, Vimal10/21/2009 7:49 AMBatchApexTerritoryAssignmentBulk 
 10/21/2009 7:49 AMBatch ApexCompleted1181180Desai, Vimal10/21/2009 7:53 AMBatchApexTerritoryAssignmentBulk 


The batch apex code is:-


global class BatchApexTerritoryAssignmentBulk implements Database.Batchable<SObject>
  public String query;

 global database.Querylocator start(Database.batchableContext bc)
   Integer cont;
   if(query == null)
   cont = 1;
   cont = 0;
   if(cont == 0)
   return Database.getQueryLocator(query);
   return null;

 global void execute(Database.BatchableContext BC, List<SObject> sobjList)
  System.Debug('SOBJECT LIST SIZE PASSED AS QUERY : ' + sobjList.size());
  TerritoryAssignmentBulk tabObj = new TerritoryAssignmentBulk();
  List<Lead> leadList = new List<Lead>();
  Map<Id,Account> accMap = new Map<Id,Account>();
  List<Opportunity> oppList = new List<Opportunity>();
  for(sobject s : sobjList)
   if(s.getsObjectType() == Lead.sObjectType)
    Lead l = (Lead)s;
  System.Debug('LEAD LIST SIZE : ' + leadList.size());
   tabObj.runRules(oppList, accMap, leadList, false);//The method that needs to be called, for lead all others will be null and boolean false
   update leadList;

  /*for(sobject s : sobjList)
   if(s.getsObjectType() == Account.sObjectType)
    Account a = (Account)s;
   tabObj.runRules(oppList, accMap, leadList, false);//The method that needs to be called, for lead all others will be null and boolean false
   update accMap.values();
  } */

 global void finish(Database.BatchableContext BC)

 webservice static String BatchApexMethod(String objectType)
  BatchApexTerritoryAssignmentBulk leadBulk = new BatchApexTerritoryAssignmentBulk();
  BatchApexTerritoryAssignmentBulk accBulk = new BatchApexTerritoryAssignmentBulk();

  if(objectType == 'Lead')

   leadBulk.query = 'Select Id, Name, Company, Keep_in_Territory__c, LastName, Status, isConverted From Lead where isConverted = false';
   System.Debug('QUERY' + leadBulk.query);
  Id leadProcessId = Database.executeBatch(leadBulk);

  /*if(objectType == 'Account')

   accBulk.query = 'Select Id, Name From Account';
  Id accountProcessId = Database.executeBatch(accBulk); */
  return '';


The custom button code is :-







var acc = "Account";

var lead = "Lead";

var opty = "Opportunity";

sforce.apex.execute("BatchApexTerritoryAssignmentBulk", "BatchApexMethod", {a:acc});

sforce.apex.execute("BatchApexTerritoryAssignmentBulk", "BatchApexMethod", {a: lead});

sforce.apex.execute("BatchApexTerritoryAssignmentBulk", "BatchApexMethod", {a: opty});







Well, you do have 'return null' in start() method. Try to change it to something like

Database.getQueryLocator('select ID from ... limit 0')

if you do not want to process any records.




We are also working with Territory Management and Batch Apex.

We have a batch apex to update a field on Account, which is used in territory rules.


When batch apex is run (from a trigger) it works fine and updates all the accounts.

However, territory rules are not run and the account is still allocated to old territory.


How can you invoke territory rules from Apex after batch apex is executed.


Thanks in advance,

Venu Adepu.