+ Start a Discussion
LinkYogiLinkYogi 

Batch class is not updating records for large number of records

Hi All,

I facing an issue that batch class is not updating records for large number of records.

Batch Class:

global class oppwithmarketing implements Database.Batchable<sObject>, Database.AllowsCallouts, Database.Stateful 
{
  global Map<string,Decimal> MrktSignals;
  global map<string,string>Dosierurl;
  global map<string,string>DosierSummaryurl;
  
  global final string query = 'select id, name, Marketing_Signal__c,dosierurl__c,dosiersummaryurl__c from opportunity';

  global Database.QueryLocator start(Database.BatchableContext BC) 
  {  
      Update_Records op = new Update_Records();
       op.OppData();
       MrktSignals =  op.MrktRatngMap.clone();
       system.debug('MrktSignals** '+MrktSignals );
       Dosierurl= op.dossierurlMap.clone();
       system.debug('Dosierurl** '+Dosierurl);
       DosierSummaryurl= op.dossierurlsummarymap.clone();
       system.debug('DosierSummaryurl** '+DosierSummaryurl);
    return Database.getQueryLocator(query);
  }

  global void execute(Database.BatchableContext BC, List<sObject> batch) 
  {    
     try
     {
       for(Sobject so :batch)
       {
           opportunity opp1 = (opportunity)so;      
           if(MrktSignals.containsKey(opp1.name) && MrktSignals.get(opp1.name) <> null){
              so.put('Marketing_Signal__c',MrktSignals.get(opp1.name));      
           }
           if(Dosierurl.containsKey(opp1.name) && Dosierurl.get(opp1.name) <> null){
          
              so.put('dosierurl__c',Dosierurl.get(opp1.name));      
           }
           if(DosierSummaryurl.containsKey(opp1.name) && DosierSummaryurl.get(opp1.name) <> null){
          
              so.put('dosiersummaryurl__c',DosierSummaryurl.get(opp1.name));      
           }
       }
         if (batch.size()>0)
         {
             Database.SaveResult[] list_save=Database.Update(batch, false);
         for (Database.SaveResult sr: list_save)
         {
             if (sr.isSuccess())
             {
                 system.debug( ' **** ' + sr.getId());
             }
             else
             {
                 for (Database.Error err: sr.getErrors())
                     system.debug(err.getStatusCode()+' **** '+err.getMessage()+ ' **** '+err.getFields());
                     //system.debug(sr.getStatusCode()+' **** '+sr.getMessage());
             }
         }
        }
     }
     catch(System.QueryException e)
     { 
         System.debug('System.QueryException on oppwithmarketing batch class ' + e);  
     }
  }

  global void finish(Database.BatchableContext BC) { 
      system.debug(LoggingLevel.WARN, 'Batch Job Complete');
  }
}

Developer Console:

Execute:
oppwithmarketing  b = new oppwithmarketing();
      database.executebatch(b);

Note: When I run the batch class 'oppwithmarketing' for 100 records, it works. But, for  500 or more records, it is not updating the records. Although, I am getting no error. 
Sumitkumar_ShingaviSumitkumar_Shingavi
Hello LinkYogi,

I think your code is eating exception or error you might be getting. I cleaned your code and you can try it to see the actual error:
global class oppwithmarketing implements Database.Batchable<sObject>, Database.AllowsCallouts, Database.Stateful {

	global Map<String,Decimal> MrktSignals;
	global map<String,string> Dosierurl;
	global map<String,string> DosierSummaryurl;

	global final String query = 'SELECT Id, Name, Marketing_Signal__c, dosierurl__c, dosiersummaryurl__c FROM Opportunity';

	global Database.QueryLocator start(Database.BatchableContext BC) {
	
		Update_Records op = new Update_Records();
		op.OppData();
		
		MrktSignals =  op.MrktRatngMap.clone();
		System.debug(LoggingLevel.INFO, 'MrktSignals** '+MrktSignals );
		
		Dosierurl= op.dossierurlMap.clone();
		System.debug(LoggingLevel.INFO, 'Dosierurl** '+Dosierurl);
		
		DosierSummaryurl= op.dossierurlsummarymap.clone();
		System.debug(LoggingLevel.INFO, 'DosierSummaryurl** '+DosierSummaryurl);
		
		return Database.getQueryLocator(query);
	}

	global void execute(Database.BatchableContext BC, List<sObject> batch) {
	
		try {
		
			for(Sobject so :batch) {			
				
				Opportunity opp1 = (opportunity) so;
				
				if(MrktSignals.containsKey(opp1.name) && MrktSignals.get(opp1.name) <> null){
					so.put('Marketing_Signal__c',MrktSignals.get(opp1.name));      
				}
				
				if(Dosierurl.containsKey(opp1.name) && Dosierurl.get(opp1.name) <> null){
					so.put('dosierurl__c',Dosierurl.get(opp1.name));      
				}
				
				if(DosierSummaryurl.containsKey(opp1.name) && DosierSummaryurl.get(opp1.name) <> null){
					so.put('dosiersummaryurl__c',DosierSummaryurl.get(opp1.name));      
				}
			}
			
			if (batch.size() > 0) {
			
				List<Database.SaveResult> list_save=Database.Update(batch, TRUE); //This will stop any error
				
				for (Database.SaveResult sr: list_save) {
					if(sr.isSuccess()) {
						System.debug( ' **** ' + sr.getId());
					} else {
						for (Database.Error err: sr.getErrors()) {
							System.debug(LoggingLevel.Error, err.getStatusCode()+' **** '+err.getMessage()+ ' **** '+err.getFields());
						}					
					}
				}
			}
		} catch(Exception e) { 
			System.debug(LoggingLevel.ERROR, 'Error : ' + e.getMessage() + ' at line number ' + e.getLineNumber());
		}
	}

	global void finish(Database.BatchableContext BC) { 
		System.debug(LoggingLevel.INFO, '==Batch Job Complete==');
	}
}

Also, Once you catch error then you can get on root cause.

I also feel that you are fetching some Opp data from "Update_Records" class; the code will do it just one time so are you sure that you get whole set of Opps to be processed for matching all criterias in one shot? If not then you might need to do it in every batch so at the beginning of execute() method.

Hope this helps!
 
LinkYogiLinkYogi
@Sumitkumar_Shingavi. Thanks a lot for your response!
I have identified in my class that I am not getting a response that is why, records are not updating. But, your response would be helpful for future problems.