Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void fire() {
- String requestId = insertEvents();
- if (requestId != null) System.enqueueJob(new FireQueueable(requestId));
- }
- public class FireQueueable implements Queueable, Database.AllowsCallouts {
- private String requestId;
- public FireQueueable(String requestId) {
- this.requestId = requestId;
- }
- public void execute(QueueableContext context) {
- if (fireFor(requestId)) {
- String nextRequestId = insertEvents();
- if (nextRequestId != null) System.enqueueJob(new FireQueueable(nextRequestId));
- }
- }
- }
- // Must use Database.AllowsCallouts for PDF generation
- public with sharing class GenerateOnePDF implements Queueable, Database.AllowsCallouts {
- public void execute(QueueableContext context) {
- Blob v = Page.myPDFPage.getContentAsPDF();
- // Do your DML here.
- }
- }
- // Limit one enqueueJob per execute.
- public with sharing class GeneratePDFFiles implements Database.Batchable<integer> {
- public Iterable<Integer> start(Database.BatchableContext context) {
- return new Integer[] { 1, 2, 3 }; // Change this logic as you like.
- }
- public void execute(Database.BatchableContext context, integer[] scope) {
- System.enqueueJob(new GenerateOnePDF()); // You'd pass your request ID, I suppose.
- }
- public void finish(Database.BatchableContext context) {
- }
- }
- <apex:page>
- </apex:page>
- public with sharing class GeneratePDFS implements Database.Batchable<Integer>, Database.Stateful, Database.AllowsCallouts {
- String priorId;
- Integer counter = 0;
- String insertEvents() {
- insert new Account(Name=counter.format());
- return ++counter > 5? null: counter.format();
- }
- public Integer[] start(Database.BatchableContext context) {
- return new Integer[] { 1 };
- }
- public void execute(Database.BatchableContext context, Integer[] scope) {
- if(priorId != null) {
- insert new Document(Name='Test Me', FolderId=UserInfo.getUserId(), Body = Page.myPDFPage.getContentAsPDF());
- }
- priorId = insertEvents();
- }
- public void finish(Database.BatchableContext context) {
- if(priorId != null) {
- Database.executeBatch(this);
- }
- }
- }
- public class FireEventsBatchable implements Database.Batchable<Integer>,
- Database.AllowsCallouts, Database.Stateful {
- // Entry point
- public static void execute(String requestId) {
- if (requestId != null) {
- Database.executeBatch(new FireEventsBatchable(requestId), 1);
- }
- }
- private String requestId;
- private FireEventsBatchable(String requestId) {
- this.requestId = requestId;
- }
- public Integer[] start(Database.BatchableContext bc) {
- // With batch size of 1 execute gets called up to 8 times
- return new Integer[] {1, 2, 3, 4, 5, 6, 7, 8};
- }
- public void execute(Database.BatchableContext bc, Integer[] scope) {
- if (requestId != null) {
- if (Events.fireFor(requestId)) {
- requestId = Events.insertEvents();
- }
- } else {
- System.abortJob(bc.getJobId());
- }
- }
- public void finish(Database.BatchableContext bc) {
- // If more to do - chain to another job
- execute(requestId);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement