1

I want a apex batch that can autorun when called another apex controller. I have tried with the apex batch code as below. But not getting the point correctly to how to put the batchscheduler. Can someone help?

The link I followed is this one. https://blog.internetcreations.com/2011/09/executing-batch-apex-in-sequence/

Created custom settings accordingly only. Nothing written as required for Scheduler. Please help me here.

Batch code:

global class CallBatch implements Database.Batchable<sObject>{

    global Database.QueryLocator start(Database.BatchableContext BC){
        return Database.getQueryLocator('SELECT Call_abv__c,Call_abv__r.Parent_Call_vod__c FROM Compliance_Incident_Transaction_abv__c where Compliance_Incident_abv__r.Status_abv__c=\'Open\'');
    }

    global void execute(Database.BatchableContext BC, List<Compliance_Incident_Transaction_abv__c> scope){

     Set<Id> CallMap= new Set<Id>();
        for (Call2_vod__c myContact : [select ID,Parent_call_vod__c from call2_vod__c where ID in:scope]) 
        {CallMap.add(mycontact.id);
         CallMap.add(mycontact.Parent_call_vod__c);
        system.debug('CallMap@'+CallMap);

        }
        List<Call2_vod__C> Call2_vod_master = new List<Call2_vod__C> (); 
        Call2_vod_master=[SELECT id, Name,Account_vod__c, Incident_Exists_Flag_abv__c, Is_Parent_Call_vod__c, Parent_Call_vod__c 
    FROM Call2_vod__c where id in :CallMap];

        for (Call2_vod__c C : Call2_vod_master )
        { c.Override_lock_vod__c=true;
          c.Incident_Exists_Flag_abv__c =true ;
        }

        update Call2_vod_master;



    }

    global void finish(Database.BatchableContext BC){
        BatchSchedule__c b = BatchSchedule__c.getOrgDefaults();
        DateTime n = datetime.now().addMinutes(5);
        String cron = '';

        cron += n.second();
        cron += ' ' + n.minute();
        cron += ' ' + n.hour();
        cron += ' ' + n.day();
        cron += ' ' + n.month();
        cron += ' ' + '?';
        cron += ' ' + n.year();

        b.scheduled_id__c = System.schedule('Batch 2', cron, new BatchScheduler2());

        update b;
    }

}
3
  • If you want batch 2 to start after batch 1, you can call batch 2 from finish method of batch 1 finish method using Database.executbatch(..). – javanoob Sep 18 '15 at 3:38
  • @javanoob I have only one batch at my requirement and I want to run that when a method called in a different apex controller. I need to start the schedule by that time only. – Testing_SFDC Sep 18 '15 at 3:43
  • you can run the batch from your main class using Database.executbatch(..) – Tushar Sharma Sep 18 '15 at 6:25
3

You could simply initiate the batch by inserting below in your apex class from where you want to execute a batch class. Id bid = Database.executeBatch(, );

e.g in your case it should be Id bid = Database.executeBatch(new CallBatch() , 200);

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.