1

We have many workflow rules on the account Object, one of it is it prevents the users from entering wrong data in to Location__c.I recently created a Trigger on the Account Object which looks for a field Location__c if it is changed I need the Trigger to be fired which makes a callout to POST this change to external system.

trigger updateLoccation Account (after update) {
 if(trigger.isupdate)
{
    set<Id> AccountIds = new Set<Id>();

    for( Id accountId : Trigger.newMap.keySet() ){
    System.debug('## Old Value of Location' +Trigger.oldMap.get(accountId).Location__c);
    System.debug('## New Value of Location' + Trigger.newMap.get(accountId).Location__c);
        if( (Trigger.oldMap.get(accountId).Location__c != Trigger.newMap.get(accountId).Location__c))
        {
            AccountIds.add(accountId); 
        }
    }

If in the UI I make the change to Location__c on the Account Record say NY to TN, the trigger is fired and the callout is made to external system. Then the Workflow rules on the Account record is executed which basically checks if the Location assigned is right and set it back to NY.Then again the trigger is fired but the old and new value of the field is NY so nothing happens. So the issue is two systems are out of sync, SF has 'NY' and External system has 'TN'. I need the external system know only when the changes happen on the field. Can anyone say why after the workflow field update I see the old and new value on the trigger same. How can I handle is this scenario any help is greatly appreciated

2
  • You can't change the order Salesforce (see Order of Execution: developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…) executes triggers, flows, and workflow rules. You can only change their conditions, enable, or disable them. This is something you will need to work out with your team to decide. Why is there a workflow rule that reverses the location change? – nbrown Jul 21 '20 at 19:51
  • @nbrown I get the order of execution but can you tell me why the new and old value in Trigger is not working when it is fired after the workflow field update is done ? – user81642 Jul 21 '20 at 19:59
2

This is working-as-designed, Triggers and Order of Execution second-to-last bullet

Exception.

When a record is updated and subsequently triggers a workflow rule field update, Trigger.old in the last update trigger doesn’t contain the version of the object immediately before the workflow update, but the object before the initial update was made. For example, suppose that an existing record has a number field with an initial value of 1. A user updates this field to 10, and a workflow rule field update fires and increments it to 11. In the update trigger that fires after the workflow field update, the field value of the object obtained from Trigger.old is the original value of 1, rather than 10, as would typically be the case.

If you want the external system to stay in sync, you need either some form of state management within the transaction or to delegate the callout to async when the transaction has ended

4
  • 2
    Change Data Capture and the Replication API are both options to consider as an alternative approach that occurs after the entire on platform transaction is complete. – Mark Pond Jul 21 '20 at 21:14
  • @cropredy Here I am not using VF page instead I am making a future callout method from the trigger when the record is updated. Looks like Async Callout works with VF Pages. Do you think I could still use Async Callout for my usecase – user81642 Jul 22 '20 at 0:24
  • Just use a future or queueqble to do the http callout – cropredy Jul 22 '20 at 3:59
  • I am already using the future callout, using the future callout only I am seeing the behavior. I posted a different question salesforce.stackexchange.com/questions/313523/… can you please help me with this – user81642 Jul 22 '20 at 4:11

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.