-1

I have a batch class in which i need to put a counter to count all the records that are processed successfully. My code is as follows:

 global class BatchUpdate implements Database.Batchable<sObject>,Database.Stateful {

    global final String Query;  
    public Integer total=0;
        global BatchUpdate(String q) {  
            Query=q;
        }  


   global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator('SELECT id,b.LastModifiedDate from Base_Catalogue__c b');
   }

   global void execute(Database.BatchableContext BC, list<sObject> scope){


        List<Base_Catalogue__c> bc1 = new List<Base_Catalogue__c>();
        List<BC_Count__c> count = [select BC_count_AP08__c from BC_Count__c];
        system.debug('The total count records from the Ap08 server: '+ count);

        system.debug('Start of the program');
        for (sObject s : scope) {
            Base_Catalogue__c bc2 =(Base_Catalogue__c)s;
            DateTime d=bc2.LastModifiedDate; 
            Date lastdate = date.newinstance(d.year(), d.month(), d.day());
            system.debug('Hi2::'+lastdate);

            DateTime dT = System.Today();
            Date today = date.newinstance(dT.year(), dT.month(), dT.day());
            system.debug('Hi3:::'+today);

             if(lastdate==today){
             system.debug('check');
              bc2.Check__c=true;  
              total++;

              }
              else
              {
              bc2.Check__c=false;
              }    
              bc1.add(bc2);

              system.debug('The total count records from the Ap08 server: '+ count);
        for(BC_Count__c var: count)
        {
            var.BC_count_AP08__c = total;
            update var;

        }

        }
        update bc1;


       }
       global void finish(Database.BatchableContext BC)
       {
           System.debug(LoggingLevel.Info, 'Your info');
           system.debug('COUNT::::'+total);
           if(!Test.isRunningtest()){
           //Updating all the Detail object records to point to Active Base Catalogue
           UpdateDetailReference udr = new UpdateDetailReference();
           Database.executebatch(udr,200);
           }

       }
    }

"total" is the counter that I've put. and when the bc2.Check__c is evaluated to true, that's when the counter should be incremented. I want to to use this counter value as a field in a object. The system.debug value is showing the correct value. However, The field BC_count_AP08__c is showing a value of 0. I think I am not assigning the value correctly.

  • The count should be in which specific record of the object? There will be many records. – sfdcFanBoy May 30 '16 at 7:17
  • The count is supposed to come in the BC_Count object that I've created. In that, the field BC count AP08 is supposed to be updated with the value of the counter "count". – Younus Zain May 30 '16 at 7:20
  • are you sure that there are records returned by query – kurunve May 30 '16 at 7:21
  • Yes yes! this a fully functional class already running! Why do u ask? – Younus Zain May 30 '16 at 7:27
  • 1
    Younus -- re: urgent -- exhortations to the community are not well-received. Please edit your question as well as cleaning up your use of a variable name considered an English language vulgarism. – cropredy May 30 '16 at 21:48
0

Although there are certain unclear things, still I would like to suggest below answer:

global class BatchUpdate implements Database.Batchable<sObject>,Database.Stateful {

    global final String Query;  
    public Integer total=0;
        global BatchUpdate(String q) {  
            Query=q;
        }  


   global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator('SELECT id,b.LastModifiedDate from Base_Catalogue__c b');
   }

   global void execute(Database.BatchableContext BC, list<sObject> scope){


        List<Base_Catalogue__c> bc1 = new List<Base_Catalogue__c>();
        system.debug('Start of the program');
        for (sObject s : scope) {
            Base_Catalogue__c bc2 =(Base_Catalogue__c)s;
            DateTime d=bc2.LastModifiedDate; 
            Date lastdate = date.newinstance(d.year(), d.month(), d.day());
            system.debug('Hi2::'+lastdate);

            DateTime dT = System.Today();
            Date today = date.newinstance(dT.year(), dT.month(), dT.day());
            system.debug('Hi3:::'+today);

             if(lastdate==today){
             system.debug('check');
              bc2.Check__c=true;  
              total++;

              }
              else
              {
              bc2.Check__c=false;
              }    
              bc1.add(bc2);           

        }
        update bc1;


       }
       global void finish(Database.BatchableContext BC)
       {
           List<BC_Count__c> count = [select BC_count_AP08__c from BC_Count__c];
           for(BC_Count__c var: count)          {
                var.BC_count_AP08__c = total;
                }
           update count;
           system.debug('The total count records from the Ap08 server: '+ count);
           System.debug(LoggingLevel.Info, 'Your info');
           system.debug('COUNT::::'+total);
           if(!Test.isRunningtest()){
           //Updating all the Detail object records to point to Active Base Catalogue
           UpdateDetailReference udr = new UpdateDetailReference();
           Database.executebatch(udr,200);
           }

       }
    } 

Below are reasons of Edit:

  1. You can maintain a count variable throughout the batch scope and then update by single DML in finish. There is no need to save DML in every batch.
  2. I have removed a DML that occurred within two loops. There were chances of failure because of governor limits.

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.