Formula Fields

Using Application Composer, you can extend an application's object model by adding fields to both standard or custom objects. One such field is a formula field, which is calculated in the runtime application using the Groovy-based expression included in the field's definition.

Formula Field Properties

Create a formula field by specifying values for the common set of field properties, such as display label and field name. You also set properties for this field that are specific to the formula field type.

The following table shows properties that are common across multiple field types:

Field Property

Field Property Region

Label

Appearance

Help Text

Appearance

Display Width

Appearance

Note: Do not use. A field's display width is dynamically calculated based on the field type and resolution.

Name

Name

Description

Name

The following table shows properties that are unique to certain field types, including formula fields:

Field Property

Field Property Region

Formula Type

Specify the field's data type, such as text, number, or date. You can specify the type only during field creation.

Field Value Type

Display Type

If the formula type is Text, then indicate if you want this formula field to render in the runtime application as a simple text box, or if the field should allow multiple lines where text can wrap.

Appearance

Depends On

Constraints

Using the Expression Builder and the Depends On Choice List

Use the Depends On choice list to indicate if the field should be automatically recalculated (using the expression you write) if another field's value changes.

This is a screenshot of the Constraints region, which you set up when defining a formula field.
Note: The Depends On choice list includes a list of fields that belong to the same object. If you want this formula field to automatically recalculate if the value of another field on a different object changes, then you must write a server script.

Use the expression builder to write an expression that calculates the field's value at runtime.

This list provides several examples of when to use the expression builder:

  • If your expression calculates the value of an employee's annual bonus amount, set the expression to automatically recalculate that amount if the employee's salary changes.

  • If your expression determines the correct customer phone number to use for an opportunity, set the expression to automatically reset the phone number if the opportunity's customer account changes.

Note: Use formula fields only for deriving a field's value, and not for setting any additional field values.

Additional Formula Field Specifications

Additional specifications for this field type include the following details:

  • Data type is set by the Formula Type property.

  • The formula field type isn't supported by subject areas. You can't add formula fields to a custom report.

  • You can't search on a formula field.

  • A formula field is a computed attribute, and exists only at runtime. This is a transient type of attribute that doesn't persist in the database as a table column. Hence, no maximum number of formula fields exists for an object.

  • A formula field's Groovy script is evaluated every time the field's value is requested by any layer. Accordingly, don't write process-intensive scripts, such as calling external web services, as this could impact the page's performance.

  • Don't use a formula field to set other fields' values because, due to the order of rendering, the order in which the fields are processed isn't guaranteed. If you want to write code that derives other field values when the value of some other field is changed, use the After Field Changed trigger documented in the Groovy Scripting Reference guide.

  • Use simple computation logic inside formula fields.
  • Use formula fields only for deriving and not for setting any additional field values.
  • Formula fields are not editable, but don't use them for displaying a custom field as read-only.
  • When writing a Groovy expression for a formula field, don't refer to transient attributes (for example, the display label of a dynamic choice list field) in the formula logic.
  • Never use a 'setAttribute()' inside a formula field. This can cause a record lock issue due to the repeated evaluation of the formula field at various times of the page rendering.
  • If a formula field calculation involves querying objects, then avoid exposing such formula fields on overview pages (Summary table) and on subtabs. If this type of formula field is exposed as a column in a table, the formula is evaluated and its query is fired for each row. This will impact the performance of a List page. If a formula involves a complex query, expose the formula field on the Object Detail page, not on the List page.
  • Avoid including formula fields in REST calls if your specific business case doesn't require you to do so. You can ensure this by clearing the Include in Service Payload check box.
  • When using formula fields to evaluate values in child collection in REST, all the formula fields in the child are evaluated for every child record causing performance degradation.

    To improve performance, instead of using formula fields to implement logic like executing an SQL query, use a persistent field like a text field and add any logic to the insert or update trigger on that field and set the value to that field.