배치 파일내에서 외부 http 통신하려면 Database.AllowsCallouts 필요하다.
// 코드 예제
public with sharing class batch_practice implements Database.Batchable<sObject>,
Database.AllowsCallouts {
public batch_practice() {
}
public Iterable<sObject> start(Database.BatchableContext bc) {
List<Account> listTarget = new List<Account>();
// http 요청
HttpRequest req = new HttpRequest();
HttpResponse res = new HttpResponse();
Http http = new Http();
String endpoint;
endpoint = 'Your endpoint url';
try {
// req.setHeader('Authorization', header);
req.setHeader('Content-Type', 'application/json');
req.setEndpoint(endpoint);
req.setMethod('POST');
req.setBody('Information you wanna send');
res = http.send(req);
String sJson = res.getBody();
} catch (Exception e) {
System.debug('error :: ' + e.getMessage());
} finally {
System.debug('finally done');
}
for (Integer i = 10; i < 20; i++) {
String name = 'batch practice ' + i;
Account acc = new Account(Name = name);
listTarget.add(acc);
}
return listTarget;
}
public void execute(Database.BatchableContext bc, List<sObject> listTarget){
Database.insert(listTarget);
}
public void finish(Database.BatchableContext bc){
}
}
참고자료
https://techdicer.com/how-to-make-http-callout-in-batch-apex-class-salesforce/