I'm struggling to make this work. I'm a very beginner developer and really need some help.

I need to connect to an external system, get the Contacts from a list and create new Leads in Salesforce. In case the Contact already exists in Salesforce, based in a custom field I created for the external Id, then I need to update it if there's something to update. I managed to connect and bring all the existing contacts to Salesforce for the first time, but now, I can't insert new ones. I'm thinking that may be I'm doing something wrong with the dml statements, but I'm lost. Also, I'm having a hard time with Salesforce limits.

I'll share the code and I hope that someone can help me make it more efficient (and actually work).

Thank you so much in advance!!!

This is the callout class:

global with sharing class SendinBlueCallOut {
    public static HTTPResponse performAction(Integer offset, Integer count){ 
            HttpRequest req = new HttpRequest();
            //WARNING!!!! Adams Salesforce API Key!!!!
            req.setHeader('api-key', 'xkeysib-7c6c6c0747c6e83dd5ee023ff78457db31ffa6........');
            req.setHeader('content-type', 'application/json');
            req.setHeader('accept', 'application/json');
            Http http = new Http();            
            HTTPResponse res = http.send(req);
            if(res.getStatusCode() == 200) {

                system.debug('Response: ' + res);
            return res;

This is the Batch Class:

global class SendinBlueBatchClass implements Database.Batchable<sObject> , Database.AllowsCallouts {
    global List<SendinBlueJsonParser.contacts> conList = new List<SendinBlueJsonParser.contacts>();
    List<SendinBlueJsonParser.contacts> conList1 = new List<SendinBlueJsonParser.contacts>();
    global String query = 'SELECT Id, FirstName, LastName, Email, LeadSource, Status, MobilePhone, SendinBlue_Id__c FROM Lead WHERE SendinBlue_Id__c != null';
    global SendinBlueJsonParser sjp;
    set<Integer> ExternalIds = new set<Integer>(); 
    global List<Lead> LeadsToInsert = new List<Lead>();
    global List<Lead> LeadsToUpdate = new List<Lead>();
    global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator(query);
    global void execute(Database.BatchableContext BC, List<Lead> scope) { 
        System.debug('Starting Execution!!!!!!!');
        Integer offSet = 0;
        Integer count = 1;
        HTTPResponse res;
        HttpRequest req = new HttpRequest();
        while ( offset < count ) {
                res = SendinBlueCallOut.performAction(offset, count);
                res = SendinBlueMockCallOutTest.respond(req);
            if(res.getStatusCode() == 200) {
                offset = offset + 50;
                SendinBlueJsonParser sjp = SendinBlueJsonParser.parse(res.getBody());
                conList1 = sjp.contacts;
                count = sjp.count;
                system.debug('Count: ' + count);
                system.debug('conList1: ' + conList.size());
            system.debug('conList: ' + conList.size());
            for (Lead l : scope) {
                for (SendinBlueJsonParser.contacts con : conList) {
                    if (l.SendinBlue_Id__c.intValue() == con.Id) {                  
                        l.FirstName = con.attributes.FirstName;
                        l.LastName = con.attributes.LastName;
                        l.Company = l.FirstName + ' ' + l.LastName;
                        l.MobilePhone = con.attributes.sms; 
                        l.City = con.attributes.city;
                        l.State = con.attributes.state;
                        l.Country = con.attributes.country;
                        l.PostalCode = con.attributes.postal_code;
                        l.Street = con.attributes.address;
                        if (!leadsToUpdate.contains(l)){
                    else {
                        Lead l1 = new Lead();
                        l1.SendinBlue_Id__c = con.Id;
                        l1.Email = con.email;
                        l1.FirstName = con.attributes.FirstName;
                        l1.MobilePhone = con.attributes.sms;
                        l1.LeadSource = 'SendinBlue';
                        l1.City = con.attributes.city;
                        l1.State = con.attributes.state;
                        l1.Country = con.attributes.country;
                        l1.PostalCode = con.attributes.postal_code;
                        l1.Street = con.attributes.address;
                        l1.Status = 'Open - Not Contacted';
                        if (con.attributes.LastName != null) {
                            l1.LastName = con.attributes.LastName;
                        else {
                            l1.LastName = 'Pending' + l1.SendinBlue_Id__c;
                        l1.Company = l1.FirstName + ' ' + l1.LastName;
                        if (!leadsToInsert.contains(l1)){
        system.debug('leadsToInsert: ' + LeadsToInsert.size());
        system.debug('leadsToUpdate: ' + LeadsToUpdate.size());
            if (LeadsToInsert != null){
                Database.Upsert(LeadsToInsert, false);
            else if (LeadsToUpdate != null) {
                Database.Upsert(LeadsToUpdate, false);
        catch (dmlexception e) {
            system.debug(e.getCause() + e.getMessage());{          
    global void finish(Database.BatchableContext BC){ 
        system.debug('THE END!!');       
  • What is the error you getting in inserting/updating contacts?
    – realfire
    May 24 at 22:06
  • @realfire depending on the amount of records, sometimes no errors, just nothing happens. When I see errors, these are about cpu time mostly. May 24 at 22:09
  • How many records you want to sync from sib to sfdc?
    – realfire
    May 24 at 22:10
  • @realfire I need to get 400o records at first, which is fine, the code do that. But then, if I add another contact in sendinblue and run the code again, nothing happens. I don't know why it does not create a new Lead as it did before with the 4000 records. May 24 at 22:29
  • 1. If job errors, you can see why it failed and error message from "Apex Jobs" screen (/apexpages/setup/listAsyncApexJobs.apexp) 2. Salesforce has lots of limits (developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…). Some of the limits coming into picture here are number of call outs per txn and cpu time. Check what is the error thrown for your jobs and then refactor to process contacts iteratively in multiple batches.
    – realfire
    May 24 at 22:36

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.