TransWikia.com

Execute trigger after the workflow rules

Salesforce Asked by user81642 on November 13, 2021

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

One Answer

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

Answered by cropredy on November 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP