세일즈포스 배치 Database.AllowsCallouts

dev_jo·2022년 6월 3일
0

세일즈포스

목록 보기
3/18
post-custom-banner

배치 파일내에서 외부 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/

profile
To be a better developer!!
post-custom-banner

0개의 댓글