Skip to main content
minor code fixes, now compiles
Source Link
sfdcfox
  • 501.6k
  • 21
  • 473
  • 828

You could use either the batchable interface or queueable interface. Here's a batchable interface implementation:

global class CommunityProcess implements Database.Batchable<ProcessItem> {
    interface ProcessItem {
         void doAction(Id recordId);
    }
    public class Phase1 implements ProcessItem {
        public void doAction(Id recordId) {
            // Implementation omitted
        }
    }
    public class Phase2 implements ProcessItem {
        public void doAction(Id recordId) {
            // Implementation omitted
        }
    }
    global Id recordId;
    global void CommunityProcess(Id recordId) {
        this.recordId = recordId;
    }
    global Iterator<ProcessItem>Iterable<ProcessItem> start(Database.BatchableContext context) {
        return new ProcessItem[] { new Phase1(), new Phase2() }.iterator();
    }
    public void execute(Database.batchableContext context, ProcessItem[] scope) {
        scope[0].doAction(recordId);
    }
    public void finish(Database.batchableContext context) {
        // Clean up
    }
}

It can be called like this:

Database.executeBatch(new CommunityProcess(someRecordId), 1);

As a queueable interface, you'd need a number of top-level classes to implement each phase, but the process would be similar in nature. Note: in a developer edition, there's a maximum of 5 chains (the initial parent, plus four children, in order).

Also note, this code isn't tested, so you might need to tweak the code a bit to achieve your specific goals.

You could use either the batchable interface or queueable interface. Here's a batchable interface implementation:

global class CommunityProcess implements Database.Batchable<ProcessItem> {
    interface ProcessItem {
         void doAction(Id recordId);
    }
    class Phase1 implements ProcessItem {
        void doAction(Id recordId) {
            // Implementation omitted
        }
    }
    class Phase2 implements ProcessItem {
        void doAction(Id recordId) {
            // Implementation omitted
        }
    }
    global Id recordId;
    global void CommunityProcess(Id recordId) {
        this.recordId = recordId;
    }
    global Iterator<ProcessItem> start(Database.BatchableContext context) {
        return new ProcessItem[] { new Phase1(), new Phase2() }.iterator();
    }
    public void execute(Database.batchableContext context, ProcessItem[] scope) {
        scope[0].doAction(recordId);
    }
    public void finish(Database.batchableContext context) {
        // Clean up
    }
}

It can be called like this:

Database.executeBatch(new CommunityProcess(someRecordId), 1);

As a queueable interface, you'd need a number of top-level classes to implement each phase, but the process would be similar in nature. Note: in a developer edition, there's a maximum of 5 chains (the initial parent, plus four children, in order).

Also note, this code isn't tested, so you might need to tweak the code a bit to achieve your specific goals.

You could use either the batchable interface or queueable interface. Here's a batchable interface implementation:

global class CommunityProcess implements Database.Batchable<ProcessItem> {
    interface ProcessItem {
         void doAction(Id recordId);
    }
    public class Phase1 implements ProcessItem {
        public void doAction(Id recordId) {
            // Implementation omitted
        }
    }
    public class Phase2 implements ProcessItem {
        public void doAction(Id recordId) {
            // Implementation omitted
        }
    }
    global Id recordId;
    global void CommunityProcess(Id recordId) {
        this.recordId = recordId;
    }
    global Iterable<ProcessItem> start(Database.BatchableContext context) {
        return new ProcessItem[] { new Phase1(), new Phase2() };
    }
    public void execute(Database.batchableContext context, ProcessItem[] scope) {
        scope[0].doAction(recordId);
    }
    public void finish(Database.batchableContext context) {
        // Clean up
    }
}

It can be called like this:

Database.executeBatch(new CommunityProcess(someRecordId), 1);

As a queueable interface, you'd need a number of top-level classes to implement each phase, but the process would be similar in nature. Note: in a developer edition, there's a maximum of 5 chains (the initial parent, plus four children, in order).

Also note, this code isn't tested, so you might need to tweak the code a bit to achieve your specific goals.

Source Link
sfdcfox
  • 501.6k
  • 21
  • 473
  • 828

You could use either the batchable interface or queueable interface. Here's a batchable interface implementation:

global class CommunityProcess implements Database.Batchable<ProcessItem> {
    interface ProcessItem {
         void doAction(Id recordId);
    }
    class Phase1 implements ProcessItem {
        void doAction(Id recordId) {
            // Implementation omitted
        }
    }
    class Phase2 implements ProcessItem {
        void doAction(Id recordId) {
            // Implementation omitted
        }
    }
    global Id recordId;
    global void CommunityProcess(Id recordId) {
        this.recordId = recordId;
    }
    global Iterator<ProcessItem> start(Database.BatchableContext context) {
        return new ProcessItem[] { new Phase1(), new Phase2() }.iterator();
    }
    public void execute(Database.batchableContext context, ProcessItem[] scope) {
        scope[0].doAction(recordId);
    }
    public void finish(Database.batchableContext context) {
        // Clean up
    }
}

It can be called like this:

Database.executeBatch(new CommunityProcess(someRecordId), 1);

As a queueable interface, you'd need a number of top-level classes to implement each phase, but the process would be similar in nature. Note: in a developer edition, there's a maximum of 5 chains (the initial parent, plus four children, in order).

Also note, this code isn't tested, so you might need to tweak the code a bit to achieve your specific goals.