0

I haven't worked on Batch/Scheduler class before.In our org we have an existing scheduler class written as below:

Apex Class:

global with sharing class SchedularForBatchApex implements Schedulable {
global void execute(SchedulableContext sc) {
    String CLASS_NAME1 = 'Actual__c';
    try {

        BatchTargtUpdate b = new BatchTargtUpdate(CLASS_NAME1);
        Database.executeBatch(b);

    }catch(Exception ex){
        system.debug('Exception in BatchTargtUpdate(Actual__c) Execute Process submision and error is--->'+ex);

        BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
    } 
    String CLASS_NAME2 = 'Revenue_Actual__c';
    try {

        BatchTargtUpdate b1 = new BatchTargtUpdate(CLASS_NAME2);
        Database.executeBatch(b1);

    }catch(Exception ex){
        system.debug('Exception in BatchTargtUpdate(Revenue_Actual__c) Execute Process submision and error is--->'+ex);

        BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
    } 
    String CLASS_NAME3 = 'Direct_Revenue_Actual__c';
    try {

        BatchTargtUpdate b = new BatchTargtUpdate(CLASS_NAME3);
        Database.executeBatch(b);

    }catch(Exception ex){
        system.debug('Exception in BatchTargtUpdate(Direct_Revenue_Actual__c) Execute Process submision and error is--->'+ex);

        BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
    } 
    String CLASS_NAME4 = 'Actual_Cost__c';
    try {

        BatchTargtUpdate b = new BatchTargtUpdate(CLASS_NAME4);
        Database.executeBatch(b);

    }catch(Exception ex){
        system.debug('Exception in BatchTargtUpdate(Actual_Cost__c) Execute Process submision and error is--->'+ex);

        BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
    } 
    String CLASS_NAME5 = 'Average_Deal__c';
    try {

        BatchTargtUpdate b = new BatchTargtUpdate(CLASS_NAME5);
        Database.executeBatch(b);

    }catch(Exception ex){
        system.debug('Exception in BatchTargtUpdate(Average_Deal__c) Execute Process submision and error is--->'+ex);

        BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
    } 
    String CLASS_NAME6 = 'Bill_Forecast_Accuracy__c';
    try {

        BatchTargtUpdate b = new BatchTargtUpdate(CLASS_NAME6);
        Database.executeBatch(b);

    }catch(Exception ex){
        system.debug('Exception in BatchTargtUpdate(Bill_Forecast_Accuracy__c) Execute Process submision and error is--->'+ex);

        BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
    } 
    String CLASS_NAME7 = 'Number_of_Won_Opportunities__c';
    try {

        BatchTargtUpdate b = new BatchTargtUpdate(CLASS_NAME7);
        Database.executeBatch(b);

    }catch(Exception ex){
        system.debug('Exception in BatchTargtUpdate(Number_of_Won_Opportunities__c) Execute Process submision and error is--->'+ex);

        BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
    } 
    String CLASS_NAME8 = 'Number_of_No_Decision_Opportunities__c';
    try {

        BatchTargtUpdate b = new BatchTargtUpdate(CLASS_NAME8);
        Database.executeBatch(b);

    }catch(Exception ex){
        system.debug('Exception in BatchTargtUpdate(Number_of_No_Decision_Opportunities__c) Execute Process submision and error is--->'+ex);

        BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
    } 
    String CLASS_NAME9 = 'Actual__c';
    try {

        BatchTargtUpdate b = new BatchTargtUpdate(CLASS_NAME9);
        Database.executeBatch(b);

    }catch(Exception ex){
        system.debug('Exception in BatchTargtUpdate(Actual__c) Execute Process submision and error is--->'+ex);

        BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
    } 
    String CLASS_NAME10 = 'Number_of_Closed_Opportunities__c';
    try {

        BatchTargtUpdate b = new BatchTargtUpdate(CLASS_NAME10);
        Database.executeBatch(b);

    }catch(Exception ex){
        system.debug('Exception in BatchTargtUpdate(Number_of_Closed_Opportunities__c) Execute Process submision and error is--->'+ex);

        BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
    } 
    String CLASS_NAME11 = 'Strategic_Account_Revenue_Actual__c';
    try {

        BatchTargtUpdate b = new BatchTargtUpdate(CLASS_NAME11);
        Database.executeBatch(b);

    }catch(Exception ex){
        system.debug('Exception in BatchTargtUpdate(Strategic_Account_Revenue_Actual__c) Execute Process submision and error is--->'+ex);

        BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
    } 
    String CLASS_NAME12 = 'Demand_Fulfillment_Actual__c';
    try {

        BatchTargtUpdate b = new BatchTargtUpdate(CLASS_NAME12);
        Database.executeBatch(b);

    }catch(Exception ex){
        system.debug('Exception in BatchTargtUpdate(Demand_Fulfillment_Actual__c) Execute Process submision and error is--->'+ex);

        BatchUtil.trackErrorLogs(CLASS_NAME12, 'Execute',
                                 'Exception Line Number:' + ex.getLineNumber() +
                                 '~ Exception Type-' + ex.getTypeName()+                                                             
                                 '~ Exception Message-' + ex.getmessage());
    } 

    try {

        SalesPersonBatchTargtUpdate b16 = new SalesPersonBatchTargtUpdate('Revenue_Actual__c','Actual_Revenue_for_oppty');
        Database.executeBatch(b16);

    }catch(Exception ex){
        system.debug('Exception in SalesPersonBatchTargtUpdate(Demand_Fulfillment_Actual__c) Execute Process submision and error is--->'+ex);

        BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
    } 

    try {

        SFDCBatchTargetUpdate b17 = new SFDCBatchTargetUpdate();
        Database.executeBatch(b17);
    }catch(Exception ex){
        system.debug('Exception in SFDCBatchTargetUpdate() Execute Process submision and error is--->'+ex);

        BatchUtil.trackErrorLogs(CLASS_NAME1, 'Execute', 'Exception Line Number:' + ex.getLineNumber() + '~ Exception Type-' + ex.getTypeName()+  '~ Exception Message-' + ex.getmessage());
    } 
}


}

if u can see 17 jobs are calling in one scheduler class.As per my knowledge(theoretical ) only 5 batch jobs can be active at the same time.there could be possibility that rest 12 jobs are in queue.Values that we are passing in CLASS_NAME1,CLASS_NAME2..... are the custom setting values. and we run this scheduler only when we receive data from 3rd party application .It does not have fix timing.we schedule this job at anytime.

I just want to know how can do chain job here.

Can someone please help me best practice of writing multiple batch job in one scheduler class?

  • Do you want the batch jobs to run concurrently or serially? if the latter, then the finish() method of job(i) starts the batch class for job (i+1). See salesforce.stackexchange.com/questions/8109/… – cropredy Sep 22 '17 at 2:05
  • I want to run it Concurrently.As when the batch job runs,it copies the data from 3rd party system to Salesforce Reports. – Dhananjay Patil Sep 22 '17 at 2:41
  • you can only run 5 jobs concurrently so if you start 19 jobs, they'll run when system has resources – cropredy Sep 22 '17 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.