Skip to content

Latest commit

 

History

History
85 lines (60 loc) · 5.51 KB

calculated_fields.md

File metadata and controls

85 lines (60 loc) · 5.51 KB

Calculated Fields

Evolve Forms allows administrators and developers to dynamically calculate any value to be rendered on the screen as if it were an actual field using Apex or Flow.

This is far more robust than normal formula fields. Calculated Fields enables rendering data calculated from child records or the result of a callout, and it does not consume any columns in the schema.

If you can compute it in Apex or Flow, then you can render it on the screen as if it were a field.

Calculated Field Architecture

Renderers

The fields will be calculated using a Renderer. A renderer will accept the current record's Id as input and return a collection of CalculatedField objects.

The CalculatedField class has public @AuraEnabled variables whose data types correspond to the public properties of the lightning-formatted-XYZ Lightning Web Component.

Data Type Lightning Web Component
LightningFormattedAddress lightning-formatted-address
LightningFormattedDateTime lightning-formatted-date-time
LightningFormattedEmail lightning-formatted-email
LightningFormattedLocation lightning-formatted-location
LightningFormattedName lightning-formatted-name
LightningFormattedNumber lightning-formatted-number
LightningFormattedPhoneNumber lightning-formatted-phone
LightningFormattedText lightning-formatted-text
lightning-formatted-rich-text
LightningFormattedTime lightning-formatted-time
LightningFormattedUrl lightning-formatted-url
Boolean lightning-input (rendered as type="checkbox")

The values in the populated data type will be rendered on the screen as if it were a field on the record, using all of the formatting capabilities of the corresponding Lightning Web Component.

Flow Renderers

To define a flow-based renderer, create an autolaunched flow with the following variable specifications:

Variable Name Variable Type Available for Input Available for Output Description
recordId text yes no The Id of the record
calculatedFields Collection of CalculatedField no yes The calculated fields to be rendered on the screen

Flow Renderer Outline

Flow Renderer Assignment

Then, add the Calculated Fields Section component to your record page and provide the flow's API name.

Contact Calculated Fields Demo

Apex Renderers

Apex renderers work very similarly to flow renderers. Create a class which implements the CalculatedFieldController.Renderer interface:

public class ContactCalculatedFields implements CalculatedFieldController.Renderer {
  public List<CalculatedField> getFields(Id recordId) {
    List<CalculatedField> result = new List<CalculatedField>();

    List<Contact> contacts = [
      SELECT Account.Parent.Name
      FROM Contact
      WHERE Id = :recordId
    ];

    String parentAccountName = contacts?.get(0)?.Account?.Parent?.Name;
    if (parentAccountName == null) {
      return result;
    }

    CalculatedField field = new CalculatedField();
    field.label = 'Account\'s Parent Account Name';
    field.text = new LightningFormattedText();
    field.text.value = parentAccountName;

    result.add(field);
    return result;
  }
}

Then, add the Calculated Fields Section component to your record page and provide the Apex class name.

Contact Calculated Fields Demo