I am requesting external application from SF on daily basis to get records and sync. I have 5-10 custom objects in our environment. I have to make API calls individually to get all those records and update them on SF. So, I am using scheduler to schedule a job and calling batch controller. I am looping through object names and batching them in sequence.


global class ScheduleGetAllRecords implements schedulable{
global void execute(SchedulableContext sc) 
    Datetime sysTime = Date.today().addDays(-1);

    //customSetting where we are saving credentials
    String username = customSetting.name;
    String password = customSetting.password__c;
    Blob headerValue = Blob.valueOf(username + ':' + password);
    String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);

    for(String s:objectList){
        system.debug('sObject name in Loop:: '+s);
        String url = customSetting.Endpoint_URL__c;
        BatchGetAllRecordsController getRecs = new BatchGetAllRecordsController(''+s,''+url, ''+authorizationHeader);


global with sharing class BatchGetAllRecordsController implements Database.Batchable<sObject>, Database.Stateful, Database.AllowsCallouts {
global string s_object;
global List<sObject> sObjectInsertRecords;
global List<sObject> sObjectUpdateRecords;
global String endpointUrl;
global final String authorizationHeader;
global map<String, list<sObject>> resultRecordList;
global BatchGetAllRecordsController(String objName, String endpointUrl, String authorizationHeader){
    this.s_object = objName;
    this.endpointUrl = endpointUrl;
    this.authorizationHeader = authorizationHeader;
global Database.QueryLocator start(Database.BatchableContext BC){
    String objectName;
    if(s_object =='customers'){
        objectName = 'Account';
    if(s_object =='orders'){
        objectName = 'Order__c';
    if(s_object =='invoices'){
        objectName = 'Invoice__c';
    if(s_object =='payments'){
        objectName = 'Payment__c';
    String query = 'SELECT id, Name FROM '+objectName;

    return Database.getQueryLocator(query);
global void execute(Database.BatchableContext BC, List<sObject> batch){
    system.debug('object Name:: '+s_object);
    boolean doesRecordsExists = true;
    Http h = new Http();
    HttpResponse resXml = new HttpResponse();
    HttpRequest req = new HttpRequest();
    req.setHeader('Authorization', authorizationHeader);
    req.setHeader('Content-Type', 'text/xml');
    resXml= h.send(req);
    String responseBody = resXml.getBody();
    system.debug('reponse body:: '+responseBody);
    FutureGetAllRecords recController = new FutureGetAllRecords();
    resultRecordList = recController.parseXml(s_object, responseBody);
    for (String keyValues : resultRecordList.keySet()){
        List<sObject> recordToInsert = new List<sObject>();
        List<sObject> recordToUpdate = new List<sObject>();
        List<sObject> recordLineItemList = new List<sObject>();
            recordToUpdate = resultRecordList.get(keyValues);
            system.debug('received recordUpdateList list size:: '+recordToUpdate.size());
            recordToInsert = resultRecordList.get(keyValues);
            system.debug('received recordInsertList size:: '+recordToInsert.size());
    insert sObjectInsertRecords;
    update sObjectUpdateRecords;
global void finish(Database.BatchableContext BC){


FutureGetAllRecords controller is just parsing XML and returning a MAP which has different lists. I need to update or Insert them accordingly. But I am unable to get orders and payments. Process is stopping after invoices. There are no queue list pending too. Where am I doing wrong. Please advice. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.