Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- global class BatchCallout implements Database.Batchable<sObject>, Database.AllowsCallouts, Database.Stateful {
- Integer successRecords = 0;
- Integer errorRecords = 0;
- Integer offset = 0;
- Integer maxOffset;
- Boolean stopCallout = false;
- String endpoint;
- global BatchCallout(Integer maxOff){
- maxOffset = maxOff;
- }
- global Database.QueryLocator start(Database.BatchableContext BC) {
- }
- global void execute(Database.BatchableContext BC, List<SObject> scope) {
- if (offset <= maxOffset){
- //make callout
- Http h = new Http();
- HttpRequest req = new HttpRequest();
- req.setMethod('GET');
- req.setEndpoint(endpoint+'?offset='+offset);
- HttpResponse res = h.send(req);
- if (res.getStatus() == 200){
- Map<String,Object> results = (Map<String,Object>)JSON.deserializeUntyped(res.getBody());
- //increment offset
- offset++;
- //make DML
- insert records
- } else {
- //make callout with same offset
- //reinvoke execute method ???
- }
- }
- }
- global void finish(Database.BatchableContext BC) {
- System.debug('Records success: '+successRecords);
- System.debug('Records error: '+errorRecords);
- public void execute(Database.BatchableContext context, List<SObject> records)
- {
- // perform callout
- // perform DML
- }
- public void execute(Database.BatchableContext context, List<SObject> records)
- {
- // perform callout 1
- // perform callout 2
- // perform callout 3
- // perform callout 4
- // perform callout 5
- // perform DML
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement