0

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

0

Issue seems to be in the trigger at below line-

if(Contact.Send_to_Onboarding_Portal__c == true)

Instead you can build a list like below :-

list<contact> lstContact = new list<contact> ();
For(Contact c : trigger.new){
if(c.Send_to_Onboarding_Portal__c){
  lstContact.add(c);
}

}

3
  • Thanks for your help @Manish, this is working when the tick box is checked upon contact creation, it fires to our endpoint / and doesnt fire if the checkbox is not checked. If I don’t initially check the box, when i create the contact and then check the box at a later date it doesn’t send? Do you know how I can trigger the API call if the checkbox is checked after contact creation? Many thanks for your help
    – con
    Jul 1 '20 at 11:41
  • You can add 'After Update' event in the trigger. Jul 1 '20 at 12:15
  • Thanks Manish, i noticed that and added it soon after. It's now hitting my endpoint upon updating the checkbox, but in the Apex Batch Jobs / status detail I'm getting the following message even though status is Success. First error: Update failed. First exception on row 0 with id 0035E000013uqk8QAA; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, LIFT_ContactTrigger: execution of AfterUpdate caused by: System.AsyncException: Database.executeBatch cannot be called ... Many thanks
    – con
    Jul 1 '20 at 12:18

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.