0

So I need to use Batch Apex to insert some new contacts into our org. This isn't an overly massive list but there are other reasons it has to be done this way in our specific situation I won't get into right now.

Basically I have some code that externally calls our data warehouse and constructs a List out of that. All of the logic is done in another class and I just want to put it into the batchable class and insert it.

But I am not really sure what I am misunderstanding here.

global class ZuoraBatch implements Database.Batchable<sObject>, Database.AllowsCallouts {

    global final List<Contact> upList = zuoraContactSync.insertNew(); //external callout of data I want

    global ZuoraBatch(List<Contact> syncNew) {
        syncNew = upList;     
    }

    global List<Contact> start(Database.BatchableContext context) {
        return Database.getQueryLocator(syncNew); //how do I just pass the list I used in the constructor here? It already has all the data I want.
    }

    global void execute(Database.BatchableContext context, List<Contact> scope) {
        Database.insert(syncNew);

    }

    global void finish(Database.BatchableContext context) {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddress = new String[] {''};
        mail.setToAddresses(toAddress);
        mail.setSubject('Batch Job for Zuora Contacts is Completed');
        mail.setPlainTextBody('Contacts Synched: ' + contactList.size());
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}
0

See the below classes where the batch class is executed in an annonmous apex as below in first block hoping that you will be doing in a scheduled apex. In here you doo not need a getQueryLocator as you are not querying anything.

Id batchJobId = Database.executeBatch(new ZuoraBatch(), 200); // OR you can pass it to the constructor if you like Database.executeBatch(new ZuoraBatch(zuoraContactSync.insertNew()), 200);

Hope this helps!

Batch Class: ZuoraBatch

global class ZuoraBatch implements Database.Batchable, Database.AllowsCallouts {

global List<Contact> upList = new List<Contact>(); //external callout of data I want

global ZuoraBatch() {
    upList = zuoraContactSync.insertNew();
}

global List<Contact> start(Database.BatchableContext BC) {
    return upList; //how do I just pass the list I used in the constructor here? It already has all the data I want.
}

global void execute(Database.BatchableContext context, List<Contact> scope) {
    Database.insert(scope);

}

global void finish(Database.BatchableContext context) {
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    String[] toAddress = new String[] {'hasanthatest123@yopmail.com'};
    mail.setToAddresses(toAddress);
    mail.setSubject('Batch Job for Zuora Contacts is Completed');
    mail.setPlainTextBody('Contacts Synched: ' + upList.size());
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}

}

Helper Class : zuoraContactSync

public without sharing class zuoraContactSync {
    public static list<contact> insertNew(){
        HttpRequest httpReq = new HttpRequest();
        httpReq.setEndpoint('https://restcountries.eu/rest/v2/all'); // this is to mimic your contact sync endpoint call
        httpReq.setMethod('GET');
        httpReq.setHeader('Content-Type', 'application/json');
        httpReq.setTimeout(1000);
        System.debug('Http Requset=' + httpReq);

        //-- 2. sending the httpRequest and getting the httpResponse
        Http http = new Http();
        HttpResponse httpRes;

        try {
        httpRes = http.send(httpReq);
        } catch (System.CalloutException e) {
        //-- Retrying the http request in case of timeout
        httpRes = http.send(httpReq);
        }


        List<Object> results = (List<Object>)JSON.deserializeUntyped(httpRes.getBody());

        list<Contact> testContactList = new list<Contact>();
        //Create Contacts in bulk.
        Integer i =0;
        for(Object key: results) {
            i++;
            Map<String, Object> rules0 = (Map<String, Object>)key;
            //System.debug('Http Response= key  ' + rules0.get('alpha2Code'));
            if(i > 5) {continue;}
            System.debug('Http Response= key  ' + rules0.get('alpha2Code'));
            Contact testContact= new Contact();
            testContact.FirstName = 'ALDIMOBILE'+rules0.get('alpha2Code');
            testContact.LastName = 'Smith'+rules0.get('alpha2Code');
            testContact.MobilePhone = '1234';
            testContactList.add(testContact);

        }
        return testContactList;
    }

}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.