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();
req.setEndpoint('https://api.sendinblue.com/v3/contacts/lists/107/contacts?limit=500&offset='+offSet+'&sort=desc');
req.setMethod('GET');
//WARNING!!!! Adams Salesforce API Key!!!!
req.setHeader('api-key', 'xkeysib-7c6c6c0747c6e83dd5ee023ff78457db31ffa6........');
req.setHeader('content-type', 'application/json');
req.setHeader('accept', 'application/json');
req.setTimeout(60000);
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();
req.setEndpoint('https://api.sendinblue.com/v3/contacts/lists/107/contacts?limit=500&offset=0&sort=desc');
req.setMethod('GET');
while ( offset < count ) {
if(!Test.isRunningTest()){
res = SendinBlueCallOut.performAction(offset, count);
}
else{
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());
}
conList.addall(conList1);
}
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)){
LeadsToUpdate.add(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)){
LeadsToInsert.add(l1);
}
}
}
}
system.debug('leadsToInsert: ' + LeadsToInsert.size());
system.debug('leadsToUpdate: ' + LeadsToUpdate.size());
try{
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!!');
}
}