Sub topic 3
Formula Fields and Choosing Salesforce Field Types
Assume you know Salesforce objects, records, fields, and basic Setup navigation. This chapter focuses on calculated and specialized fields that affect data storage, security, and integrations.
Formula fields
A formula field calculates a value from other fields, functions, or related records. Users do not enter its value manually. Salesforce evaluates it when the record is accessed.
The formula’s return type determines how Salesforce treats the result. Common return types include Text, Number, Checkbox, Date, and Currency. Picklist values require ISPICKVAL, not a plain equality comparison.
IF(ISPICKVAL(StageName, "Closed Won"), Amount, 0)Formula fields are useful for values such as account age, risk flags, or calculated totals. Because the result is calculated, changing source fields changes the displayed result.
Roll-up summary fields
A roll-up summary field calculates values from related detail records. It works on the parent side of a master-detail relationship and can count records or calculate SUM, MIN, or MAX.
Use a roll-up when the relationship and calculation fit Salesforce’s declarative limits. A lookup relationship cannot normally host a roll-up summary. In that case, Apex or an aggregate SOQL query may be required.
Security and integration concerns
Encrypted text protects sensitive values from ordinary viewing, but it limits searching, sorting, and some automation. Apply field-level security as well. Encryption does not replace access control.
Mark a field as Unique when Salesforce must reject duplicate values. Mark it as an External ID when an outside system uses the value to match records. External ID fields are indexed, which can improve selective queries and upserts. These settings do not make a field secret.
Changing a field type can convert values, reject records, remove formatting, or break formulas, flows, Apex, reports, and integrations. Review dependencies and back up data before changing types. Create a new field when conversion risk is high.
Inspecting types and choosing
Apex can inspect the declared type:
Schema.DescribeFieldResult revenue = Account.AnnualRevenue.getDescribe();
System.debug(revenue.getType()); // CURRENCYMetadata exposes settings such as type, precision, and scale:
<type>Number</type>
<precision>5</precision>
<scale>2</scale>Choose a formula for derived values, a roll-up for master-detail aggregates, encrypted text for restricted text, and Unique or External ID for identity and integration keys. Choose the simplest field that preserves the required data and future relationships.
Exercise
Which field should calculate the total Opportunity Amount on an Account with a lookup relationship?
Show answer
Use Apex or an aggregate SOQL query, because roll-up summary fields require a master-detail relationship.