0

My second inner class code is not executing.

public class MyQueueable1 implements Queueable {

     Public Static list < Account > acclist = new list < Account > ();
        public MyQueueable1() {

        }
        public void execute(QueueableContext context) {

            System.enqueueJob(new MyQueueable2());
        }
       //This class is not executing..
        public class MyQueueable2 implements Queueable {

            public MyQueueable2() {

            }
            public void execute(QueueableContext context) {
                List < Account > a = [SELECT Id, Name
                                      FROM Account WHERE Id =: acclist];
            }
        }
    }

From Developer Console:

MyQueueable1 my = new MyQueueable1(); // enqueue the job for processing ID jobID = System.enqueueJob(my);

1
1

I use this ChainedQueueable abstract class:

public abstract class ChainedQueueable implements Queueable {

    private Set<Id> recordIds;

    public ChainedQueueable(Set<Id> recordIds) {
        this.recordIds = recordIds;
    }

    public abstract void processRecord(Id recordId);

    public abstract ChainedQueueable createJob(Set<Id> ids);

    public void execute(QueueableContext context) {

        if (recordIds.size() == 0) {
            return;
        }

        Id recordId = getNextInvoiceId();

        processRecord(recordId);

        processNextInvoice();
    }

    private void processNextInvoice() 
    {
        enqueueJob(this.recordIds);
    }

    private void enqueueJob(Set<Id> ids) {

        if (ids.size() == 0) {
            return;
        }

        // create next job
        ChainedQueueable job = createJob(ids);

        // queue next job
        Id jobId = System.enqueueJob(job);

        System.debug(jobId);
    }

    private Id getNextInvoiceId() {

        // get next record
        Id recordId;
        for(Id item : recordIds) {
            recordId = item;
            break;
        }

        // remove recordId
        recordIds.remove(recordId);

        return recordId;
    }
}

You implement it like this:

public class ExampleChainedQueueable extends ChainedQueueable {

    public ExampleChainedQueueable(Set<Id> recordIds) {
        super(recordIds);
    }

    public override void processRecord(Id recordId) {
        // do work here
    }

    public override ChainedQueueable createJob(Set<Id> ids) {
        return new ExampleChainedQueueable(ids);
    }
}

And call it like this:

Set<Id> recordIds = new Set<Id>();
new ExampleChainedQueueable(recordIds).enqueueJob();
1
  • Clever implementation!
    – Raul
    Sep 3 '21 at 10:00
0

I've had problems before where Salesforce won't execute an inner class that implements Batchable, you might have the same issue here.

To see if it is that, try moving it to it's own class and see if that works.

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.