Activity Forums Salesforce® Discussions How to schedule a batch class without scheduler class in salesforce?

  • Prachi

    Member
    November 15, 2019 at 4:31 pm

    Hi Deepak,

    You can schedule a batch class from UI. Enter Apex classes in quick find box than click on Schedule Apex.

    Thanks.

    • This reply was modified 4 years, 5 months ago by  Prachi.
  • Yogesh

    Member
    November 18, 2019 at 8:29 am

    Hello ,

    you can get help from this code below:-

    public class keepTruckInBatchForVehicleUpadte implements database.Batchable<decimal>, Database.AllowsCallouts, database.stateful, schedulable

    {

    public  List<decimal> pageList;

    public  boolean executeNext;

    public  List<Vehicle_Update__c> lastvehicleUpdates;

    public keepTruckInBatchForVehicleUpadte()

    {

    executeNext=false;

    pageList = new List<decimal>();

    }

    public keepTruckInBatchForVehicleUpadte(List<decimal> counts, boolean bool)

    {

    executeNext = bool;

    pageList = new List<decimal>();

    pageList.addAll(counts) ;

    }

     

    public iterable<decimal> start(Database.BatchableContext BC)

    {

    return pageList;

    }

    public void execute(Database.BatchableContext BC, List<decimal> scope)

    {

    decimal count = scope[0];

    keepTruckInApiCallouts.getVehicleLocations(count);

    string returnResponse = keepTruckInApiCallouts.getVehicleLocations(count);

    keepTruckInApiCallouts.keepTruckVehicleUpdate(returnResponse);

     

    // Deserializes the returnResponse JSON string into collections of primitive data types.

    Map<String, Object> jsonObjects = (Map<String, Object>) JSON.deserializeUntyped(returnResponse);

    Map<String, Object> paginationobjs = (Map<String, Object>)jsonObjects.get('pagination');

    decimal totalRecords = integer.valueof(paginationobjs.get('total'));

    decimal perPageRecs = integer.valueof(paginationobjs.get('per_page'));

    decimal totalPages = math.ceil(totalRecords/perPageRecs);

    if(totalRecords > perPageRecs && executeNext == true)

    {

    pageList.clear();

    for(count = 2; count <= totalPages; count++)

    {

    pageList.add(count);

    }

    }

    else

    {

    executeNext = false;

    }

    }

     

    public void finish(Database.BatchableContext BC)

    {

    if(executeNext == true)

    {

    database.executeBatch((new keepTruckInBatchForVehicleUpadte(pageList, false)),1);

    }

    }

     

    public void execute(SchedulableContext SC)

    {

    lastVehicleUpdates = [select id from Vehicle_Update__c];

    delete lastVehicleUpdates;

    database.executebatch((new keepTruckInBatchForVehicleUpadte(new List<decimal>{1}, true)),1);

    }

    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos