---
schema_version: "1"
id: 747aa822db4f06945dad86538431f6100ed3f4b698fe5c4e1545a9b0a2ac5a64
title: FormulaEval Namespace
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexref/apex_namespace_formulaeval.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_namespace_formulaeval.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 1846615e5ad09800e9e0f3dd56281aeb8ec5ee8ea4cf6495feae0a0854c70188
description: The FormulaEval namespace provides classes and methods to evaluate dynamic formulas for SObjects and Apex objects. Use the methods to avoid unnecessary DML statements to recalculate formula field values or evaluate dynamic formula expressions.
---
# FormulaEval Namespace

The FormulaEval namespace provides classes and methods to evaluate dynamic formulas for SObjects and Apex objects. Use the methods to avoid unnecessary DML statements to recalculate formula field values or evaluate dynamic formula expressions.

When using a formula against an SObject or Apex object as the context object, the class methods or properties referenced by the formula must be global.

Account myAcc = new Account(Name='123');
       FormulaEval.FormulaInstance ff = Formula.builder()
           .withType(Schema.Account.class)
           .withReturnType(FormulaEval.FormulaReturnType.STRING)
           .withFormula('name & " (" & website & ")"')
           .build();
 
//Use the list of field names returned by the getReferenced method to generate dynamic soql
       String fieldNameList = String.join(ff.getReferencedFields(),',');
       String queryStr = 'SELECT ' + fieldNameList + ' FROM Account LIMIT 1'; //select name, website from Account
       Account s = Database.query(queryStr, AccessLevel.USER\_MODE);
       System.debug(ff.evaluate(s));

For usage notes, see [Formula Evaluation in Apex](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexcode.meta/apexcode/apex_formulaeval.htm "HTML (New Window)").

The following are the classes and enums in the FormulaEval namespace.

- **[FormulaBuilder Class](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_formulaeval_FormulaBuilder.htm#apex_class_formulaeval_FormulaBuilder)**  
    Contains methods to build and validate user-defined formulas.
- **[FormulaGlobal Enum](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_enum_formulaeval_FormulaGlobal.htm)**  
    Specifies a global variable that references data in your organization in the withGlobalVariables(formulaGlobals) method.
- **[FormulaInstance Class](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_formulaeval_FormulaInstance.htm#apex_class_formulaeval_FormulaInstance)**  
    Contains a method to evaluate the formula instance.
- **[FormulaReturnType Enum](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_enum_formulaeval_FormulaReturnType.htm)**  
    Specifies the return type for the withReturnType(returnType) method.