1

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?

3

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
}
  • Thank you @Bachovski. In the apex class, I'm querying the emails, attachments etc. So each time the class is executed, 7 queries run for each case and hitting the limit. – Seesh Aug 8 '14 at 2:51
  • 1
    You need to move out those queries and map the results inside your batch. Use Database.Stateful and run those queries into the start() method. How to use Database.stateful - developer.salesforce.com/forums?id=906F00000008zJ5IAI – Boris Bachovski Aug 8 '14 at 3:02
  • wow. I'll try to modify and map the results. But can I move all those queries in to batch start method and call the apex class in the execute method as you've answered? – Seesh Aug 8 '14 at 3:14
  • I believe if you map out those queries and put them in static variables, you should be able to reference them in your method. As I said, without looking at you code I can't resolve your problem. – Boris Bachovski Aug 8 '14 at 3:19

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.