I am getting the above error when I try to test my scenario which is changing the sales rep name on the salesrepmapping object. My Apex batch class is as follows

//global class UpdateShippingAddress_Batch implements Database.Batchable<sObject>,Database.AllowsCallouts{
global class UpdateShippingAddress_Batch implements Database.Batchable<sObject>  {  
    List<Shipping_Address__c> shippingList = new List<Shipping_Address__c>();
    List<Shipping_Address__c> updateList = new List<Shipping_Address__c>();

    //global UpdateShippingAddress(List<Shipping_Address__c> shippingAddressList,lIST<Sales_Territory_Mapping__c> SHIPtERRlISTALL)
    global UpdateShippingAddress_Batch (List<Shipping_Address__c> shippingAddressList)
        shippingList = shippingAddressList;
        system.debug('Updating global class shippingList '+shippingList.size());

    global Iterable<sObject> start(Database.BatchableContext BC)
        return DataBase.getQueryLocator([SELECT Id,ISAL_M_ID__c,ISAL_CS_ID__c,Sales_Rep_CS_Name__c ,Sales_Rep_M_Name__c    FROM Shipping_Address__c WHERE Id IN : shippingList]);        

    global void execute(Database.BatchableContext BC,List<Shipping_Address__c> scopeShippingAdress)
         /*for (Integer i=0;i<scopeShippingAdress.size();i++){
         system.debug('Updating global class'+updateList);
         update updateList;*/
         update shippingList;

    global void finish(Database.BatchableContext BC)

I am calling the batch in the Class below

public class SalesRepMapping_ShippingAddress {
public static void updatesalesRepMapping(List<Sales_Territory_Mapping__c > salesList,Map<Id,Sales_Territory_Mapping__c > salesNewMap,Map<Id,Sales_Territory_Mapping__c > salesOldMap){     
    system.debug('entering to the class');
    set<String> soldInTerName = new set<String>();  
    List<Sales_Territory_Mapping__c> shipTerr = new List<Sales_Territory_Mapping__c>();
    List<Shipping_Address__c> shipAdd = new List<Shipping_Address__c>();
    List<Shipping_Address__c> finalshipAdd = new List<Shipping_Address__c>();

    for(Sales_Territory_Mapping__c st : salesList){        
        if(st.SoldInTerritoryId__c!=Null && salesOldMap.get(st .id).Sales_Rep__c!=salesNewMap.get(st .id).Sales_Rep__c ){        
    system.debug('>>>>>>>>>>>>>>>>>Sales_Territory_Mapping__c soldInTerName'+soldInTerName);
    system.debug('>>>>>>>>>>>>>>>>>Sales_Territory_Mapping__c shipTerr'+shipTerr);
for(Shipping_Address__c sa : [SELECT Id, Name, Territory__c, Sales_Rep_CS_Name__c 
FROM Shipping_Address__c WHERE Territory__c In: soldInTerName]){                                    

    String query='SELECT Id, Name, Territory__c, Sales_Rep_CS_Name__c,Sales_Rep_M_Name__c,ISAL_CS_ID__c,ISAL_M_ID__c    FROM Shipping_Address__c WHERE Territory__c In: soldInTerName limit 50000';
    List<Shipping_Address__c> saList = new List<Shipping_Address__c>();

    for(Shipping_Address__c sa :saList){                                    
    system.debug('>>>>>>>>>>>>>>>>>Shipping_Address__c shipAdd'+shipAdd);

    for(Sales_Territory_Mapping__c salesTer : shipTerr){
        for(Shipping_Address__c salesAdd :shipAdd){
            //  salesAdd.Sales_Rep_CS_Name__c = salesTer.Sales_Rep__c ;

            if(salesTer .Role__c=='FSALM'){
                salesAdd .Sales_Rep_M_Name__c = salesTer.Sales_Rep__c;
                //a.AccTerritoryNameM__c = salesRepMapping.SalesTerrMapName__c;

            if(salesTer .Role__c=='FSALCS'){
                salesAdd .Sales_Rep_CS_Name__c = salesTer.Sales_Rep__c;
                //a.AccTerritoryNameCS__c = salesRepMapping.SalesTerrMapName__c;

            if(salesTer .Role__c=='ISALCS'){
                salesAdd .ISAL_CS_ID__c = salesTer.Sales_Rep__c;

            if(salesTer .Role__c=='ISALM'){
                salesAdd .ISAL_M_ID__c = salesTer.Sales_Rep__c;           

    //update finalshipAdd;
    //Database.ExecuteBatch(new UpdateShippingAddress_Batch (shipAdd,shipTerr));
    Database.ExecuteBatch(new UpdateShippingAddress_Batch (finalshipAdd),100);


Any help is appreciated.

  • 1) How are you calling this batch class 2) And y r u using Database.getQuerylocator in start ? You can simply have return [soql qurey]
    – sf_user
    Sep 15, 2016 at 20:02
  • @pmvsdt: I am using the code in other class. Can I add the class here in the comment box.
    – user31302
    Sep 15, 2016 at 22:14
  • 1
    @Harvey Ideally, you should always put all the relevant information/code in your question. You can edit your question and update your problem's description based on feedback or things that you see that can be improved in order to have a better understanding of your question. Save comments for small/quick clarifications.
    – Joca
    Sep 15, 2016 at 22:23
  • Got it, I have edited the question and added the class where I am calling the batch class. Thanks Joca.
    – user31302
    Sep 15, 2016 at 22:26

1 Answer 1


The problem is most likely heap size (Even though you are not using stateful.


If a batch apex class implements Database.Stateful, the instance state is stored in the database. At this time we do a check against the size of the instance state and the exception is thrown when the Apex Heap Size limit is exceeded. For synchronous Apex the heap size limit is 6 MB and for asynchronous Apex the limit is 12 MB. This exception can obviously occur in a scenario where large collections are persistently maintained

In some cases Database.Stateful isn't being implemented or the steps above are not helpful in working around this exception. If you've attempted these workarounds and are still seeing this exception, please follow the steps below to find where the heap size is being exceeded.

Now, there is what appears to be un-necessary use of heap size, namely the Lists in the constructor. Try changing to the below and see if it resolves your issue

global class UpdateShippingAddress_Batch implements Database.Batchable<sObject> {

Set<ID> recordIds;

global UpdateShippingAddress_Batch(Set<ID> shippingAddressIds) {
    recordIds = shippingAddressIds;

global Iterable<sObject> start(Database.BatchableContext BC) {
    return DataBase.getQueryLocator([SELECT Id, ISAL_M_ID__c, ISAL_CS_ID__c, Sales_Rep_CS_Name__c, Sales_Rep_M_Name__c FROM Shipping_Address__c WHERE Id IN :recordIds]);

global void execute(Database.BatchableContext BC, List<Shipping_Address__c> scopeShippingAdress) {
    //.....Do what you need here.....
    update scope;

global void finish(Database.BatchableContext BC) {



Now you may need to do additional queries, or pass the records to a separate processing method. Hard to tell what else you need as in your for loop you were getting values from an object not in your code so I do not know how to help you with that part.

You may also find the answer here of use (Your question may be a duplicate of this as well): batchable instance is too big

  • 1
    and ... the global modifier is unnecessary, public will do
    – cropredy
    Sep 16, 2016 at 4:18

You must log in to answer this question.