Saturday, December 13, 2014

Batch Apex Class For Real Time Scenario.................


global class MyBatchClass implements Schedulable, Database.Batchable<sObject> {

    global static void scheduleJob() {
        // 1st digit = seconds
        // 2nd digit = minutes
        // 3rd digit = hours (24 hours)
        // 4th digit = day of month (? = no value, * = all, 1 - 31)
        // 5th digit = month (? = no value, * = all, 1=January, etc.)
        // 6th digit = day of week (? = no value, * = all, 1=Sunday, etc.)

        String sch_1stDayofMonth_1am = '0 0 2 * * ?'; // 2am every day
        System.schedule('AccountPlan_Opportunity_Batch' + (Test.isRunningTest() ? 'TEST' : ''),
            sch_1stDayofMonth_1am, new AccountPlan_Opportunity_Batch());    
    }

  public static Set<Id> childAccountIdSet = new Set<Id>();
  public static Set<Id> secondLevelIdSet = new Set<Id>();
 
    public String qrystr = 'SELECT ID, Name, Account__c, Account__r.Id, Account__r.Name,  '+
        ' Account__r.CP_Hierarchy_Level_Type__c, RecordType.Name, '+
        ' (Select Id, Name, RecordTypeId, Account_Plan__c, Close_Date__c, Opportunity__c, Potential_CV__c, Product__c, '+
        '     Stage__c, NonSFDC_OpportunityName__c, NonSFDC_PotentialCV__c, Region__c, Owner__c, Expected_Close_Month__c, '+
        '    Probability__c, Incumbent__c, Strategic_Remarks__c From Top_Opportunities__r), '+
      ' (Select Id, Name, Account_Plan__c, Opportunity__c, Opportunity_Owner__c, Win_Date__c, Customer_Value__c, Incumbent__c, '+
      '    Strategic_Remarks__c From Closed_Won_Opportunities__r) '+
        ' FROM Account_Plan__c ';


    public void setQrystr(String instr) {
        qrystr = instr;
    }
   
   
    /**
     * Constructor
     * - Does nothing because nothing to do for scheduled job
     */
    global AccountPlan_Opportunity_Batch(){
    
    }
   
    /**
     * Schedule Interface Method
     * Responsible for starting the job (not batched though)
     *
     * Keep the batch size small - some queries occur inside batch
     */
    global void execute(SchedulableContext sc) {
      System.Debug('    ####MyBatchClass(sc)...');
         Database.executeBatch(new MyBatchClass(), 10);
    }

    global void finish(Database.BatchableContext BC){ }

    global Database.QueryLocator start(Database.BatchableContext BC){
        // Select all Accounts and process
      System.Debug('   #### in start...');
      Integer year = Date.today().year();
      String yearString = String.valueOf(year);
      String conj = 'WHERE ';
      if (qrystr.contains('WHERE '))
        conj = ' AND ';
      qrystr = qrystr + conj + ' Account_Plan_Year__c = \''+yearString+'\' ';
        return Database.getQueryLocator(qrystr);
    }

    global void execute(Database.BatchableContext BC, list<sObject> scope){
      System.Debug('    #### MyBatchClass(bc, scope)...');
        runJob(scope);
    }

    public static void runJob(list<sObject> scope) {
      System.Debug('    #### in MyBatchClass...');
     MyBatch1Class apctrl = new MyBatch1Class();// calling another class
     
    apctrl.createTopPipelineOpportunities(scope);
    apctrl.createClosedWonOpportunities(scope);
     
    }

   
 
 }

No comments:

Post a Comment