1

We are trying to create an Apex batch job, but the start() method never seems to be called. The class we are using for the job has the following code:

global class BatchableScorer implements Database.Batchable<sObject>, Database.Stateful {
    global List<Contact> passedContacts;

    global BatchableScorer(List<Contact> passedContacts) {
        System.debug('Constructor called');
        System.debug(passedContacts);
        this.passedContacts = passedContacts;
    }

    public Database.QueryLocator start(Database.BatchableContext bc) {
        System.debug('start() called');
        return Database.getQueryLocator([SELECT Id, FirstName FROM Contact]);
    }

    global void execute(Database.BatchableContext bc, List<sObject> scope){
        System.debug('execute() called');
        for (sObject c : scope) {
            System.debug(c);
        }
    }

    global void finish(Database.BatchableContext bc){
        System.debug('finish() called');
            AsyncApexJob job = [SELECT Id, Status, NumberOfErrors,
                JobItemsProcessed,
                TotalJobItems, CreatedBy.Email
                FROM AsyncApexJob
                WHERE Id = :bc.getJobId()];
    }
}

The code we are using to start up the job is:

BatchableScorer bct = new BatchableScorer(passedContacts);

ID batchprocessid = Database.executeBatch(bct);
List<AsyncApexJob> myrunningjob = [Select Id, Status, ApexClass.Name From AsyncApexJob where Id=:batchprocessid];
System.debug(myrunningjob);

When we add the job via executeBatch, we see the constructor gets called and the debug output shows that we create a job, with an ID, with a state of 'Holding'.

When we check the Apex Jobs page there is nothing in the list, and the start() method never gets called (we don't see any debug message).

Can anyone suggest what might be going on here? Are we missing something?

EDIT: In response to questions / comments by Ajay, I just want to be clear: right now, the code executes correctly in the context of 'Execute Anonymous'. However it does not run from the VFP controller, even though it's the same code and we do see the constructor called correctly (start is not being called when in the context of the controller).

4
  • If the status is holding, then you can check the apex flex queue. There is a very good possibility that you fill find the job over there. Please check if this helps. I'll more info to this
    – Samir
    Commented Sep 5, 2017 at 14:45
  • looked into the flex queue, not showing up there either, weird it gets the batch process id but nothing else seems to happen Commented Sep 5, 2017 at 16:11
  • any chance your List<Contact> passedContacts is empty? Commented Sep 6, 2017 at 1:35
  • unless you are writing a managed package, the batchable class and all of the methods can be declared public, passedContacts should be private
    – cropredy
    Commented Sep 9, 2017 at 22:34

2 Answers 2

0

Please try once this code.

global class BatchableScorer implements Database.Batchable<sObject>, Database.Stateful {

    public Database.QueryLocator start(Database.BatchableContext bc) {
        System.debug('start() called');
        return Database.getQueryLocator([SELECT Id, LastName FROM Contact]);
    }

    global void execute(Database.BatchableContext bc, List<sObject> scope){
        System.debug('execute() called');
        for (sObject c : scope) {
            System.debug(c);
        }
    }

    global void finish(Database.BatchableContext bc){
        System.debug('finish() called');
        AsyncApexJob job = [SELECT Id, Status, NumberOfErrors,
                            JobItemsProcessed,
                            TotalJobItems, CreatedBy.Email
                            FROM AsyncApexJob
                            WHERE Id = :bc.getJobId()];
    }
}

Execute anonymous

BatchableScorer bs = new BatchableScorer();
Database.executeBatch(bs);

Regards,
Ajay

1
  • Interesting. When we run the code from Execute anonymous, everything works as expected. We can pass in a list of Contacts to the constructor and we see the start / execute / finish methods called. However, it still does not work from the VFP (we only see the constructor called). Commented Sep 6, 2017 at 13:15
-1


I am using your code and it’s executed batch. Please use this piece of code in execute anonymous. Maybe you were not passed List of Contact in Constructor.

List<Contact> conList = new List<Contact>(); conList.add(new Contact(LastName='Test')); insert con; BatchableScorer bs = new BatchableScorer(conList); Database.executeBatch(bs);
enter image description here


I hope it will help you.

Regards,
Ajay

3
  • They are not even using the passedContacts in the batch. The start method has its own query
    – Eric
    Commented Sep 6, 2017 at 6:39
  • Hi Eric, Actually you are using 'passedContacts' in batch class constructor as a parameter which is list<Contact> type. so i'm creating first List<Contact> and passing list<contact> variable in the batch class contructor. If you want to use start method query then there is no need to pass parameter in the batch class constructor. Commented Sep 6, 2017 at 10:33
  • what I mean is in the op's code they never use passedContacts so nothing to do with it could be the cause. Their start uses a simple query
    – Eric
    Commented Sep 6, 2017 at 12:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .