0

I have scenario 1 - A formula field on Account (custom_type__c) which references the Contact's email as - Primary_Contact__r.Email Whenever Primary Contact's email id is changed , Custom Type is updated on Account, which fires account trigger. However, Trigger.Old doesn't retain the old values. Example- If Primary Contact's email id is abc@xyz.com , Custom Type on Account-abc@xyz.com. Now, email id of Primary Contact's is updated to def@xyz.com , so Custom Type on Account0 def@xyz.com , which fires account trigger. When I print below-

System.debug('In After Update NEW ' + a.custom_type__c); System.debug('In After Update Old ' + Trigger.OldMap.get(a.Id).custom_type__c)

Both the above statements print-def@xyz.com . 1)Why Trigger.OldMap() doesn't retain the old values?

I have scenario 2- I have changed the reference of the same formula field(custom_type__c) to the account name.Now Custom_Type__c = Name (Refers name of the account), in this case Trigger.OldMap retains the old values of the name.Why does it retain in scenario 2 but not in scenario 1?

  • I don't think Scenario 1's custom_type__c is a formula field. Changes to the values of formula fields do not fire triggers. Is this value updated by a trigger, or perhaps a workflow rule or process? – David Reed May 7 at 12:53
  • In Scenario 1, custom_type__c = Primary_Contact__r.Email ,is a formula field. Primary Contact is lookup to contact , on account. I update email id on Primary Contact manually , which in turn updates custom type with the new value, and trigger on account fires. – Manish A May 7 at 13:41
2

This is the expected behavior. In Account trigger only changes to Account trigger Context records can be checked.

Therefore, if you want to handle changes for Primary_Contact fields, please consider writing code in Contact trigger (presumably Primary_Contact__c is a lookup to Contact).

Explanation:

Scenario 1:

  1. Contact Record is updated.
  2. Contact triggers are run.
  3. Contact record changes are committed.
  4. Account trigger is triggered. Here old values for the Contact are not present.

Scenario 2:

  1. Account record is updated.
  2. Account trigger is run. Old values for the formula are present because it references Account fields.
  3. Account record changes are committed
  • Yes. It makes sense. – Manish A May 7 at 17:49

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.