Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. public static void fire() {
  2. String requestId = insertEvents();
  3. if (requestId != null) System.enqueueJob(new FireQueueable(requestId));
  4. }
  5.  
  6. public class FireQueueable implements Queueable, Database.AllowsCallouts {
  7. private String requestId;
  8. public FireQueueable(String requestId) {
  9. this.requestId = requestId;
  10. }
  11. public void execute(QueueableContext context) {
  12. if (fireFor(requestId)) {
  13. String nextRequestId = insertEvents();
  14. if (nextRequestId != null) System.enqueueJob(new FireQueueable(nextRequestId));
  15. }
  16. }
  17. }
  18.  
  19. // Must use Database.AllowsCallouts for PDF generation
  20. public with sharing class GenerateOnePDF implements Queueable, Database.AllowsCallouts {
  21. public void execute(QueueableContext context) {
  22. Blob v = Page.myPDFPage.getContentAsPDF();
  23. // Do your DML here.
  24. }
  25. }
  26.  
  27. // Limit one enqueueJob per execute.
  28. public with sharing class GeneratePDFFiles implements Database.Batchable<integer> {
  29. public Iterable<Integer> start(Database.BatchableContext context) {
  30. return new Integer[] { 1, 2, 3 }; // Change this logic as you like.
  31. }
  32. public void execute(Database.BatchableContext context, integer[] scope) {
  33. System.enqueueJob(new GenerateOnePDF()); // You'd pass your request ID, I suppose.
  34. }
  35. public void finish(Database.BatchableContext context) {
  36.  
  37. }
  38. }
  39.  
  40. <apex:page>
  41. </apex:page>
  42.  
  43. public with sharing class GeneratePDFS implements Database.Batchable<Integer>, Database.Stateful, Database.AllowsCallouts {
  44. String priorId;
  45. Integer counter = 0;
  46. String insertEvents() {
  47. insert new Account(Name=counter.format());
  48. return ++counter > 5? null: counter.format();
  49. }
  50.  
  51. public Integer[] start(Database.BatchableContext context) {
  52. return new Integer[] { 1 };
  53. }
  54. public void execute(Database.BatchableContext context, Integer[] scope) {
  55. if(priorId != null) {
  56. insert new Document(Name='Test Me', FolderId=UserInfo.getUserId(), Body = Page.myPDFPage.getContentAsPDF());
  57. }
  58. priorId = insertEvents();
  59. }
  60. public void finish(Database.BatchableContext context) {
  61. if(priorId != null) {
  62. Database.executeBatch(this);
  63. }
  64. }
  65. }
  66.  
  67. public class FireEventsBatchable implements Database.Batchable<Integer>,
  68. Database.AllowsCallouts, Database.Stateful {
  69.  
  70. // Entry point
  71. public static void execute(String requestId) {
  72. if (requestId != null) {
  73. Database.executeBatch(new FireEventsBatchable(requestId), 1);
  74. }
  75. }
  76.  
  77. private String requestId;
  78.  
  79. private FireEventsBatchable(String requestId) {
  80. this.requestId = requestId;
  81. }
  82.  
  83. public Integer[] start(Database.BatchableContext bc) {
  84. // With batch size of 1 execute gets called up to 8 times
  85. return new Integer[] {1, 2, 3, 4, 5, 6, 7, 8};
  86. }
  87.  
  88. public void execute(Database.BatchableContext bc, Integer[] scope) {
  89. if (requestId != null) {
  90. if (Events.fireFor(requestId)) {
  91. requestId = Events.insertEvents();
  92. }
  93. } else {
  94. System.abortJob(bc.getJobId());
  95. }
  96. }
  97.  
  98. public void finish(Database.BatchableContext bc) {
  99. // If more to do - chain to another job
  100. execute(requestId);
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement