I'm trying to call an Apex Class when a checkbox is marked true either on the SalesForce client front end checkbox or when multiple accounts and contacts are uploaded via CSV & DataLoader when the checkbox is true.
The class and trigger are working and hitting our endpoint to create accounts, but I only want the API call to happen once a custom Contact field "Send_to_Onboarding_Portal__c" is true.
Currently, I'm getting an error: "Comparison arguments must be compatible types: Schema.SObjectField, Boolean"
Any help would be very much appreciated.
trigger LIFT_ContactTrigger on Contact (after insert) {
if(Contact.Send_to_Onboarding_Portal__c == true) {
List<Contact> lstContact = Trigger.New;
Integer numContacts = lstContact.size();
LIFT_ContactBatchCreation cbc = new LIFT_ContactBatchCreation(lstContact);
id batch = DataBase.executeBatch(cbc,1);
}
}
Apex Class that should get called once checkbox is true
/* 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.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 {
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){
}
}
Many thanks in advance