I believe I’ve identified a bug in Salesforce related to formula fields. Can some of my fellow developers, and admins too, confirm if I am observing unexpected behavior? I do have a workaround.
The issue is reproducible in the following manner in any developer sandbox using the Account and Contact standard objects:
Create a custom formula field on the Account object with the following attributes:
Field Label = Test Formula Test Feld - Account Field Name = Test_Formula_Text_Field Field API Name = Test_Formula_Text_Field__c Type = Text Formula = "Test Value From Account"
Create a custom formula field on the Contact object with the following attributes:
Field Label = Test Related Formula Text Field - Acct Field Name = Test_Related_Formula_Text_Field_Acct Type = Text Formula = Account.Test_Formula_Text_Field__c
Ensure to add the custom formula field to the Contact page layouts for ease of replicating issue.
Create a Contact with no value in the Account field
Navigate to the new Contact and look at the “Test Related Formula Text Field – Acct” field
Expected Behavior: No value is display in the “Test Related Formula Text Field – Acct” field since there is no value in the Account field on the Contact record.
Actual Behavior: The value “Test Value From Account” is displayed.
Additional Notes:
The unexpected behavior occurs when using other field types like Time.
The expected behavior occurs when the field on Account (or the related object of the lookup field) is a picklist (even with a default value), and the resulting formula on Contact encases the picklist field in the TEXT formula function.Workaround:
Until this issue is resolved, the workaround is to add logic to display a null/blank value when the lookup field is null.
Example formula workaround for our test scenario:IF( NOT(ISBLANK(AccountId) ), Account.Test_Formula_Text_Field__c, NULL )
Screenshot of Example:
Answer
I believe that this is expected. The reason why is that Salesforce presumes the default value for a field when no object is present. That can make all the difference when you do something like adding a custom currency field with a default, non-zero value, and then try to do math with it in another formula in a child object. I’m not sure why Salesforce does this, but I do know it that it does do this.
You should definitely check if a lookup field is not null before using a value from the parent field, especially when that value is a formula or a field that has default values. I’m not even sure that this is documented anywhere, but I have personally observed this in other projects, so I’m not even bothering to build a version in my org to “prove” this, because I feel like that’s actually the behavior I’ve observed before.
Attribution
Source : Link , Question Author : Coogle , Answer Author : sfdcfox