50

Using the new method on sObject recalculateFormulas() fails when the Sobject includes relationship fields - note account.name in the query.

FATAL_ERROR System.UnexpectedException: Opportunity: bad field names on insert/update call: Account

Opportunity[] oList = Database.query('select id, account.name, amount from Opportunity limit 1');
for (Opportunity o : oList)
    o.recalculateFormulas();

If I try using inline SOQL; same error

Opportunity[] oList = [select id, account.name, amount from Opportunity limit 1];
for (Opportunity o : oList)
    o.recalculateFormulas();

and it fails when using child relationships as well:

FATAL_ERROR System.UnexpectedException: Opportunity: bad field names on insert/update call: OpportunityLineItems

Opportunity[] oList = [select id, (select id from OpportunityLineItems), amount from Opportunity limit 1];
for (Opportunity o : oList)
    o.recalculateFormulas();

Looks like a bug to me as otherwise if restricted to only 'pure' sObjects, code will break as soon as SOQL statements are extended with innocuous relationship fields. Unless someone knows otherwise, bug report will be filed to SFDC Support

UPDATE ...a few days later

Curiously, relationship names in a query for Contact do not generate the error

Contact[] cList = [select id , account.name from Contact limit 10] ;
for (Contact c: cList)
   c.recalculateFormulas();

nor is there an error with relationship names on a Case query

Case[] csList = [select id , account.name from Case limit 10] ;
for (Case cs: csList)
    cs.recalculateFormulas();

or (also works fine)

Case[] csList = [select id , (select id from CaseComments)  from Case limit 10] ;
for (Case cs: csList)
   cs.recalculateFormulas();

BUT .. relationship names on Custom Objects fail as they did on the Opportunity example:

Quote__c[] qList = [select id, opportunity__r.amount from Quote__c limit 10];
for (Quote__c q : qList)
    q.recalculateFormulas();

SFDC Support says this support ticket is "interesting"

Update 2015-12-04 - Per SFDC T3 Support: "This seems to be progressing in the direction of getting identified as a bug"

0

2 Answers 2

27

SFDC R&D has confirmed a bug in recalculateFormulas method . As of 0900h Pacific 2015-12-09, Known Issue not yet created.

From Support:

For now as a work around we would request you not select any lookup fields in the SOQL query when using the recalculateFormulas()

From R&D:

This issue happens if there are fields which are not updatable in SOQL.For example, if there are the fields which refer to the other object's field like account.name, it is not directly updatable, so it causes the error.If you specify the lookup field itself like accountId, it is updatable, it is OK, but if the field is not updatable like master-detail field, auto-populated lookup field, it causes the error.Some reason, if the lookup field doesn't have value like accountId = null, this error doesn't happen even if the field account.name is in SOQL.Auto number field causes the same issue because it is not updatable.

The workaround is do not put this kind of fields (not updatable) in SOQL.

This workaround, as obvious as it sounds, is not always going to be practical as one's SObject fetched from SOQL frequently has lookup fields or child relationships. Even worse, perfectly working code could break in difficult to understand ways if in the future, one innocuously adds lookup fields or relationship fields to the SOQL (say, to get the Opportunity Owner Name to display on a VF page)

I'll post updates if and when I get a scheduled fix date or release.

Here is the Known Issue prompted by the above. The more folks mark this as "it affects me", the more likely it will be the issue is fixed.

3
  • similar thing happened to me, but no SOQL in my case, so no workaround for me.
    – hlopetz
    Commented Feb 14, 2018 at 12:24
  • The workaround has been update to Use Formula.reclaculateFormulas() API. It worked for me. Commented Dec 30, 2020 at 17:35
  • 1
    @JuanAlvarez - I see that note in the KI -- could you please provide a new answer to this question and post a code snippet in the answer demonstrating the workaround?
    – cropredy
    Commented Dec 30, 2020 at 17:57
3

The Salesforce issue posted by cropredy was updated and has a new workaround which worked for me.

Instead of doing:

for (Opportunity o : oList)
    o.recalculateFormulas();

do

Formula.recalculateFormulas(oList);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .