function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Aaron BishopAaron Bishop 

Allow an update trigger to update fields on the original object.

Hey All,

I've created a trigger and a couple of methods to do the following: When a contact is created or updated, Salesforce sends an outbound HTTP request to POST data to another location. Salesforce then updates a field on that original contact with part of the response supplied by the other side. The methods themselves seem to be working just fine, but when I call them from the trigger, the field on the contact doesn't get updated. 

My thought is that perhaps Salesforce isn't allowing the update because it is trying to avoid an infinite loop (an update to the contact calls the trigger, which updates the contact, which calls the trigger, etc.). I've included the basics of my code below, and I appreciate any feedback given.
trigger ContactTrigger on Contact (before insert, before update) {
    for(Contact c : trigger.new){
        Integration.startContactSync(new List<String> {c.Id});
    }
}


global class Integration{
    
    @future(callout=true)
    public static void startContactSync(List<String> conId){
        Contact con = [SELECT Id, Foreign_Key__c FROM Contact WHERE Id = :conId.get(0)];

        if(String.isBlank(con.Foreign_Key__c )) {
            system.debug('calling SurefireConnection.createContact()');
            createContact(con.Id);
        }
        else {
            system.debug('calling SurefireConnection.updateContact()');
            updateContact(con.Id);
        }
    }

    public static String createContact(Id conId){
        Contact con = [SELECT Id, Foreign_Key__c FROM Contact WHERE Id = :conId];
        ...  
        //Set up HTTP POST request. This works.
        ...
        con.Foreign_Key__c = http.send(req);
        update con;
        return 'Success';
    }

    public static String updateContact(Id conId){
        Contact con = [SELECT Id, Foreign_Key__c FROM Contact WHERE Id = :conId];
        ...  
        //Set up HTTP POST request. This works.
        ...
        return 'Success';
    }
Thanks in advance for the help.