I have a custom Apex Trigger which calls on an Apex class to send contacts and accounts to hit an endpoint on our Server. This was working correctly until I tried to make edits to the trigger. I have since reverted back to the previous version and now when i create an account and contact on the sandbox front end the trigger is no longer firing and hitting our end point. The trigger is set to active but it doesn't seem to fire? Im new to sales force development so any help would be much appreciated.
trigger LIFT_ContactTrigger on Contact (after insert) {
List<Contact> lstContact = Trigger.New;
Integer numContacts = lstContact.size();
// TODO - Add logic here if desired to only use batch when records > 50
//if numContacts is less than 50 then use async furure method
//if numContacts is greater than 50 then use async batch Apex
//TODO - move to TriggerHandler class and only execute when Role = 'Glofox Champion'
LIFT_ContactBatchCreation cbc = new LIFT_ContactBatchCreation(lstContact);
id batch = DataBase.executeBatch(cbc,1);
}
This is the class which was sending the details from the contact and account creation on the front end.
/* TODO - Add description of class, author & date */
/* TODO - Add TEST class */
global class LIFT_ContactBatchCreation implements Database.Batchable<sObject>, Database.AllowsCallouts {
private List<Contact> lstContact;
public LIFT_ContactBatchCreation(List<Contact> lsacc) {
System.debug('constructor batch class, ' + lsacc.size());
this.lstContact = lsacc;
}
global Database.QueryLocator start(Database.BatchableContext bc) {
System.debug('start batch class, ' + lstContact.size());
String soqlQuery = 'SELECT Account.Branch_Id__c, Account.Parent_Account_Name__c, Account. Account.Date_became_Customer__c, Account.Go_Live_Date__c, Account.Name, FirstName, LastName, Email, Id From Contact where Id IN :lstContact';
return Database.getQueryLocator(soqlQuery);
}
global void execute(Database.BatchableContext bc, List<Contact> scope){
System.debug('execute batch class, ' + scope.size());
for (Contact contact : scope) {
try {
/* TODO - Remove debug statements */
System.debug(contact.Account.Name);
System.debug(contact.Account.Branch_Id__c);
String accountName = contact.Account.Name;
String branchId = contact.Account.Branch_Id__c;
String firstName = contact.FirstName;
String lastName = contact.LastName;
String email = contact.Email;
Date goLiveDate = contact.Account.Go_Live_Date__c;
Date startDate = contact.Account.Date_became_Customer__c;
String parentAccountName = contact.Account.Parent_Account_Name__c;
//TODO - move to separate class for the callout
Http http = new Http();
HttpRequest request = new HttpRequest();
//TODO - make named credential more generic so can easily be deployed between environments
request.setEndpoint('callout:Glofox_Portal/api/branches/fromSalesforceDemo');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
// Set the body as a JSON object
request.setBody('{"branchId":"'+branchId+'", "parentAccountName":"'+parentAccountName+'", "name":"'+accountName+'", "goLiveDate":"'+goLiveDate+'","startDate":"'+startDate+'" ,"admins":[{"firstName":"'+FirstName+'","lastName":"'+LastName+'","email":"'+email+'"}]}');
HttpResponse response = http.send(request);
System.debug(response.getBody());
// Parse the JSON response
if (response.getStatusCode() != 200) {
//TODO - insert error handling and/or retyr logic here
//TODO - log exception in Salesforce including error code and reason
System.debug('The status code returned was not expected: ' + response.getStatusCode() + ' ' + response.getStatus());
} else {
//TODO - update status field in Salesforce on success and update externalId
System.debug(response.getBody());
}
}
catch (Exception e) {
System.debug('Error-' + e.getMessage());
//Log exception
}
}
update scope;
}
global void finish(Database.BatchableContext bc){
}
}
try / catch
blocks around your batch classexecute
method code and you'll probably see your error directly in the org's batch job logs