I created a visualforce page which gets accessed via a custom button on the opportunity layout. When clicking the button, the opportunity ID is appended to the URL so that I can associate the next page (Account Plan.. child of master-detail relationship w/ opportunity) with it. Now, on this custom Account Plan object, we have multiple formula fields which simply lookup the value on the opportunity. A problem arises if there was not previously an account plan created and linked to that opportunity -- when I instantiate a new account plan, it does not recognize the formulas. However, when I press 'save' and my controller extension returns to the same page after upserting the records, the fields auto populate as expected. Does anyone have any ideas as to how I could work around this? Any would be appreciated.

1 Answer 1


You can't evaluate cross-object formulas on a record that does not exist in the database. You'll have to insert it and then query the record back. One "trick" that I've used in the past is to insert a record, query it back, then roll back the transaction:

Savepoint sp = Database.setSavePoint();
insert someRecord;
someRecord = [SELECT ... FROM SomeObject WHERE Id = :someRecord.Id];

The record won't be persisted, but you'll have full access to the cross-object formula values that you're looking for.

  • Right -- only this won't work, as we are not going to require the users to click save before actually filling out the form, only to populate data. Also, DML is not allowed in the constructor. Do you know of any other possible workarounds? I was thinking a trigger after update/insert of opportunity, but that won't work for static opportunities already in our system. Thanks for the reply.
    – caseyjg
    May 30, 2017 at 21:55
  • @caseyjg You can DML inside the page's action method: <apex:page standardController="xyz" extensions="myExtension" action="{!fakeInsertActionPlan}"> ...
    – sfdcfox
    May 30, 2017 at 21:57
  • That sounds like what I am looking for. I appreciate your time.
    – caseyjg
    May 30, 2017 at 21:58
  • On the salesforce.developer.com documentation, it explicitly says under the apex:page action attribute "Important: This action should not be used for initialization or DML." Is this a cause for concern?
    – caseyjg
    Jun 1, 2017 at 13:09

You must log in to answer this question.

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