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.
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:
The record won't be persisted, but you'll have full access to the cross-object formula values that you're looking for.