3

I created three formula fields on a custom object.

  1. Formula(Date) references parent objects
  2. Formula(Time) references parent objects
  3. Formula(DateTime) references formula field 1 & 2.

When I query for these fields using SOQL, field 1 & 2 return their value. Field 3 returns NULL. If I open up the record, there is a value in the field.

Does anyone know why this is happening or what I can do to fix it?

I have tried using Formula.recalculateFormulas(). When I performed this on a list i received an 'Unexpected Exception'.

When I called recalculateFormulas() on a single Object in the list, the value was now a valid date time (not null)

See the following code snippet:

List<cust)obj__c> listCustomObj = [Select Id, Formula1__c, Formula3__c FROM cust_obj__c];
system.debug(listCustomObj[0].Formula3__c) //null
//List<FormulaRecalcResult> results = Formula.recalculateFormulas(listCustomObj); //throws unexpected exception
listCustomObj[0].recalculateFormulas();
System.debug(listCustomObj[0].Formula3__c) //correct value

My questions are:

  1. Why is the initial query returning null instead of the calculated value?
  2. Why does the recalculateFormulas() method fail when called on a list?
  3. Any way you can recommend (without changing my formula fields) that I can solve this?
2
  • 1
    Is this possibly related to this known issue? Feb 24 '20 at 16:21
  • Negative Drew. I don't believe the issue i am experiencing correlates to the known issue you tagged.
    – Z33dawg
    Feb 24 '20 at 17:20
2

Why is the initial query returning null instead of the calculated value?

I remember a long time ago there was a bug where you had to query the fields that the formula was referencing in order for it to evaluate. Perhaps this a regression? I'd recommend contacting support to confirm. Also, try using the Query Editor in the Developer Console to see if the value shows up null there as well (it shouldn't).


Why does the recalculateFormulas() method fail when called on a list?

It shouldn't fail, but it won't help, either.

This method doesn’t recalculate cross-object formulas. If you call this method on objects that have both cross-object and non-cross-object formula fields, only the non-cross-object formula fields are recalculated. --Documentation

Consider logging a bug report with support if you have an error code.


Any way you can recommend (without changing my formula fields) that I can solve this?

It should work. I think your best choice is to contact support and try to get help.

Also, follow the Known Issue that Drew Kennedy linked in the comments (thanks, Drew!).

4
  • Same behavior is happening in query editor. It doesn't appear (after testing) as if you need to query the fields the formula is referencing either. Ill log a case with Salesforce, was just trying to avoid doing that :S. Always seems like more trouble than its worth.
    – Z33dawg
    Feb 24 '20 at 17:23
  • in response to saying that 'it wont help either', the reason I want to use recalculateFormulas() is because after I call this method on a record, formula 3 evaluates correctly. I can then use this value in apex logic. I dont know the implications of calling this method iteratively on a list of records. I would be more comfortable calling it on the whole list (as salesforce guides you to)
    – Z33dawg
    Feb 24 '20 at 17:26
  • @Z33dawg Each call consumes one SOQL query, as if you'd performed another query. You're absolutely right that you should only be calling this method on a list instead of one at a time.
    – sfdcfox
    Feb 24 '20 at 17:27
  • apparently this method does not affect SOQL Limits (if all info is present on sObject) --> developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… I still believe that calling it on each record is definitely NOT the way to go
    – Z33dawg
    Feb 24 '20 at 17:30
0

Question 1:

This happened to me as well with date-time fields... we will have to implicitly convert the field to string for debugging purposes..

System.debug(obj) usually converts objects to a string using built-in string conversion functions but there is no inbuilt function for date/time.

String.valueOf(obj.formula3)

should do the trick for you in debug...

could you please try... Hope this helps 😀

2
  • Thanks for the answer. Casting to a String still returns Null though
    – Z33dawg
    Feb 24 '20 at 17:21
  • That's new to me.. I will also be interested in knowing the root cause 😯
    – Abhijith E
    Feb 25 '20 at 8:56

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.