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
Ashu sharma 38Ashu sharma 38 

Issue in Batch apex

Hi,

As I am not able to update the records usng batch apex and I am schdeule it.


below is code:
====
Batch apex
global class UpdateSalesTermStatusBatch implements database.Batchable<sobject> {
    
    global database.QueryLocator start(database.BatchableContext bc){
        //query all records from database.
        return Database.getQueryLocator('select id,name,Sales_Cycle_Start_Date__c,Sales_Cycle_End_Date__c,Sales_Term_Status__c from Partner_Sales_Term__c');
        
    }
    
    global void execute(Database.BatchableContext BC, List<Partner_Sales_Term__c> scope){

        for(Partner_Sales_Term__c pst:scope){
            if(system.today() > pst.Sales_Cycle_End_Date__c){
                pst.Sales_Term_Status__c='Completed';
                system.debug('====Completed=====' +pst.Sales_Term_Status__c);
            }
            else if(system.today()<pst.Sales_Cycle_Start_Date__c){
                pst.Sales_Term_Status__c='Planned';
                system.debug('===Planned===' +pst.Sales_Term_Status__c);
            }
            else if(system.today()>=pst.Sales_Cycle_Start_Date__c && system.today() <=pst.Sales_Cycle_End_Date__c){
                pst.Sales_Term_Status__c='Current';
                system.debug('===Current====' +pst.Sales_Term_Status__c);
            }
            update scope;
        }
    }
    global void finish(Database.BatchableContext BC){
    }   
}
Schedule Apex:
global class UpdateSalesTermStatusBatchSchedulable implements Schedulable {
    
    public static String sched = '0 5 * * * ?';  //Every Hours at  
    global static String scheduleMe() {
        UpdateSalesTermStatusBatchSchedulable SC = new UpdateSalesTermStatusBatchSchedulable(); 
        return System.schedule('My batch Job', sched, SC);
    }
    global void execute(SchedulableContext sc) {
        
        UpdateSalesTermStatusBatch b1 = new UpdateSalesTermStatusBatch();
        ID batchprocessid = Database.executeBatch(b1,200);           
    }
}

And in anonmyous window
UpdateSalesTermStatusBatchSchedulable.scheduleMe(); 

Thanks