[SalesForce] Invoke an apex class from Batch class

I've created an Apex class to perform some calculations in the Cases. I would like to schedule it every day. I'm trying to achieve this using a Batch class since the volumes of data is exceeding the Governor's limit. I've developed a class with schedulable interface to invoke the batch class. I've created a batch class with the query in the start method and AsyncApexJob in the finish method. I just wanted the batch class execute method to call the apex class. Is it possible to just call an apex class from a Batch class?

Best Answer

You can definitely call another apex method from another class. Not knowing what your code looks like, I can't point out if there are going to be issues with governor limits etc. In your execute method just simply call your other apex method, passing the batch list of records as a parameter:

Batch:

// some code ...

global void execute(Database.BatchableContext BC, List<sObject> scope)
{
    // if public/global and not static
    YourOtherClass instance = new YourOtherClass();
    instance.myMethod((List <Case>)scope);

    // if static
    // YourOtherClass.myMethod((List <Case>)scope);
}

// some code ...

And your other class' method should look like this:

public static void(List <Case> cases)
{
    // do something with the cases
}
Related Topic