1

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){
    }
}
3
  • 1
    comment out the try / catch blocks around your batch class execute method code and you'll probably see your error directly in the org's batch job logs
    – Mark Pond
    Jun 30, 2020 at 20:42
  • Thanks, Mark, I now see I have a ton of errors in the Batch Apex job queue which I was unaware of? the error is "First error: only aggregate expressions use field aliasing" - at least I can see some sort of error now thanks, still unsure as to what that means. much appreciated
    – con
    Jun 30, 2020 at 20:52
  • turns out the issue was a rogue 'Account.' in the SQL query
    – con
    Jun 30, 2020 at 21:06

1 Answer 1

1

There is a spurious space character or duplicated field path element in your Dynamic SOQL query:

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';

You do not need Dynamic SOQL here; you can pass a Static SOQL query directly to Database.getQueryLocator(), which will result in the compiler checking your SOQL syntax at compile time.

Further notes:

  • As Mark pointed out in comments, you should remove try/catch blocks that swallow exceptions. This is a pattern that hurts you. Exceptions should be thrown or meaningfully handled, not hidden.
  • If you are not building a managed package and fully cognizant of the consequences of using the global modifier, this class and its methods do not need to be global.
2
  • I just spotted this thank you for your help.
    – con
    Jun 30, 2020 at 21:08
  • Thanks for your comments David, I'm a novice to SalesForce development so it's much appreciated.
    – con
    Jun 30, 2020 at 21:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .