How can I get the result of a formula ("field update"/"workflow rule"), in Apex, without hard-coding it?

There is a field whose value (calculated on upsert) is the formula result. I have dismissed inserting a new record or updating a dummy record to get the solution, because the complexity of the org would have unacceptable ripple effects.

To answer responses asking why... My Apex function is used by a visualforce page to dynamically display the formula result (as the user is typing, not during submission).

To answer responses directing to meta data... I need to be able to calculate the formula's result, not just access the formula's text.

Thank you very much in advance.

  • 1
    One way to do it would be insert, query and then rollback to a save point prior to the insert. Commented May 1, 2013 at 17:56

3 Answers 3


I do not think this is possible. My understanding is that formulas are always calculated when a record is retrieved so even if the field were available (say duplicated on the object in question) the value would not be accurate.

I think you will have to code a copy of the formula into your controller and have the controller do the calculation for you.

  • Thank you for the quick response. That is my backup plan if I can't find another way to make this work. Commented May 1, 2013 at 18:05
  • 1
    I have done this before and eventually we decided to choose a few formulas that needed to calculate instantly, and wrote the formulas within code.
    – Phil Rymek
    Commented May 1, 2013 at 18:19
  • 2
    I'm sorry to hear this. Seems like there should be a System.calculateFormula(sobject, formula) Commented Feb 5, 2015 at 22:09

Seems like Salesforce listened. You can now do it using the method recalculateFormulas() in the sObject class.

Contact obj = new Contact();
// Calculate formulas on the Contact object

You do not need to perform a DML to get the updated calculation.

Note: This method will not calculate cross-object formulas, only non-cross-object formula fields will be calculated. This also counts against an SOQL query. Having cross-object formulas being calculated as well would have been great but at least its a start.

Link to official documentation.


You're pretty much out of luck here. As techtrekker suggested you could do this by inserting and rolling back, or simply inserting, querying and then deleting — that could have knock on effects of course if you have other triggers and the like.

Since you want realtime feedback I'd be inclined to just copy the formula calculations straight into some javascript which is run in an onChange handler for the field in question. If you don't want to copy the logic and you can abuse the database then maybe use a javascript remoting solution as that wouldn't detract from the user experience and is much faster than calling controller actions.

You must log in to answer this question.

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