Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. global class BatchCallout implements Database.Batchable<sObject>, Database.AllowsCallouts, Database.Stateful {
  2.  
  3. Integer successRecords = 0;
  4. Integer errorRecords = 0;
  5. Integer offset = 0;
  6. Integer maxOffset;
  7. Boolean stopCallout = false;
  8. String endpoint;
  9.  
  10. global BatchCallout(Integer maxOff){
  11. maxOffset = maxOff;
  12. }
  13.  
  14. global Database.QueryLocator start(Database.BatchableContext BC) {
  15.  
  16. }
  17.  
  18. global void execute(Database.BatchableContext BC, List<SObject> scope) {
  19. if (offset <= maxOffset){
  20. //make callout
  21. Http h = new Http();
  22. HttpRequest req = new HttpRequest();
  23. req.setMethod('GET');
  24. req.setEndpoint(endpoint+'?offset='+offset);
  25. HttpResponse res = h.send(req);
  26. if (res.getStatus() == 200){
  27. Map<String,Object> results = (Map<String,Object>)JSON.deserializeUntyped(res.getBody());
  28.  
  29. //increment offset
  30. offset++;
  31.  
  32. //make DML
  33. insert records
  34. } else {
  35. //make callout with same offset
  36. //reinvoke execute method ???
  37. }
  38. }
  39. }
  40.  
  41. global void finish(Database.BatchableContext BC) {
  42. System.debug('Records success: '+successRecords);
  43. System.debug('Records error: '+errorRecords);
  44.  
  45. public void execute(Database.BatchableContext context, List<SObject> records)
  46. {
  47. // perform callout
  48.  
  49. // perform DML
  50. }
  51.  
  52. public void execute(Database.BatchableContext context, List<SObject> records)
  53. {
  54. // perform callout 1
  55. // perform callout 2
  56. // perform callout 3
  57. // perform callout 4
  58. // perform callout 5
  59.  
  60. // perform DML
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement