0

I made a Batch class and Schedule to call that class. However, I am not able to test in Execute Anonymous Window.

schedule

global class UpdateUserDataSchedule implements Schedulable {
     global void execute (SchedulableContext sc) {
         UpdateUserDataBatch u = new UpdateUserDataBatch ();
         database.executebatch ((u), 5);
        
     }
}

Batch

global void execute (Database.BatchableContext BC, List <User> scope) {
// ...
}

Execute Anonymous Window:

UpdateUserDataShedule.execute(Database.BatchableContext BC, List <User> scope);

Error: Line: 1, Column: 30 Unexpected token '('.

1

This isn't valid Apex and is not how you invoke a batch class:

UpdateUserDataShedule.execute(Database.BatchableContext BC, List <User> scope);

To run a batch class, create an instance of the batch and call Database.executeBatch(), just like you do in your Schedulable class:

     UpdateUserDataBatch u = new UpdateUserDataBatch ();
     Database.executeBatch (u, 5);

To directly call the execute() method, you would need to instantiate a Database.BatchableContext object (or simply pass null if, like most batch classes, you don't use it) and provide a List<User>. However, one typically does not call execute() directly, outside of some unit testing scenarios.

If you wanted to execute your scheduler class synchronously (i.e., without actually scheduling it), you'd have to create a new instance and call its execute() method:

new UpdateUserDataSchedule().execute(null);

As a side note, unless you are building a managed package and deliberately are exposing these classes outside that package, none of these items should be global.

| improve this answer | |
  • Thanks it worked using: new UpdateUserDataSchedule().execute(null); – Juliana Mar 31 at 12:33
0

The schedulable class should have the form you have described.

The basic content of the batch class should be of the form:

global without sharing class BatchProcessName implements Database.Batchable<SObject>
{

    global Database.QueryLocator start(Database.BatchableContext bc)
    {
        string qry = 'SELECT id FROM somewhere';
        return Database.getQueryLocator(qry);
    }

    global void execute(Database.BatchableContext bc, list<SObject> listVariable)
    {
        //clever code goes here using listVariable
    }

    global void finish(Database.BatchableContext bc)
    {
        // execute any post-processing operations
    }

}

Then to call it from your dev console's execute anonymous window:

BatchProcessName b = new BatchProcessName();
database.executeBatch(b,1);

It's not clear from your example whether the batch is declared properly; with "global" access modifiers.

Hope that helps.

| improve this answer | |
  • 1
    global access is neither required nor recommended for a batch class unless it is part of a managed package and intended to be accessible outside that package. – David Reed Mar 30 at 22:22
  • 1
    There's also no reason to implement the Database.Stateful marker interface if you have no instance variables. – David Reed Mar 30 at 22:23
  • David, take both your points entirely. My code was extracted from a managed package and wasn't entirely cleaned up. Though the original question did indicate global modifiers. – Simman Mar 31 at 10:16

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.