Sub topic 17
Seeing Field Types in Apex, SOQL and Metadata
This chapter assumes you know how to open Setup and Object Manager, and that you can read short Apex snippets. It shows three places where a field's type becomes visible: Apex describe calls, SOQL filters, and metadata files. The UI can hide a field's real type, and these three views show it.
Reading a type with Apex describe
Every field has a describe result. It reports the type Apex uses, which may differ from what a page layout suggests.
Schema.DescribeFieldResult revenue = Account.AnnualRevenue.getDescribe();
System.debug(revenue.getType()); // CURRENCY
Schema.DescribeFieldResult accountType = Account.Type.getDescribe();
System.debug(accountType.getType()); // PICKLISTgetType() returns a Schema.DisplayType value such as CURRENCY, PICKLIST or STRING. Use it when code must behave differently by type, for example in a generic tool that formats values.
Reading a type in metadata
Metadata files store each custom field as XML. The type element names the field type, and other elements carry the settings that type needs.
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Priority_Score__c</fullName>
<label>Priority Score</label>
<type>Number</type>
<precision>5</precision>
<scale>2</scale>
<required>false</required>
</CustomField>Here precision is the total number of digits and scale is how many of them fall after the decimal point. This field holds values up to 999.99.
Type-specific rules in formulas and SOQL
Some types need special syntax. A picklist value cannot be compared with a plain equals sign in a formula. Use ISPICKVAL.
IF(ISPICKVAL(StageName, "Closed Won"), Amount, 0)This formula on Opportunity has a Currency return type.
Multi-select picklists use INCLUDES in SOQL.
SELECT Id, Name FROM Contact WHERE Interests__c INCLUDES ('Golf;Tennis', 'Chess')A semicolon inside one string means both values must be selected. Separate strings mean either option. This query returns contacts who chose both Golf and Tennis, or who chose Chess.
Working around a missing roll-up
Roll-up summary fields need a master-detail relationship. If your relationship is a lookup, an aggregate query can compute the same total in code.
List<AggregateResult> totals = [
SELECT AccountId, SUM(Amount) total
FROM Opportunity
WHERE AccountId != null
GROUP BY AccountId
];
for (AggregateResult ar : totals) {
System.debug(ar.get('AccountId') + ': ' + ar.get('total'));
}This computes the total when queried. It is not stored on the parent, so it cannot appear in a list view or report filter the way a roll-up field can.
Common errors
- Comparing a picklist with
=in a formula. UseISPICKVAL. - Writing
Interests__c = 'Golf'in SOQL for a multi-select picklist. UseINCLUDESorEXCLUDES. - Assuming the UI label tells you the type. A field labeled "Status" may be a picklist, text or checkbox. Check describe or metadata.
- Reading
precisionas digits after the decimal point. That isscale.
Exercise
A Number field has precision 6 and scale 2. What is the largest value it can hold, and which SOQL operator filters a multi-select picklist?
Show answer
The largest value is 9999.99, because 6 total digits with 2 after the decimal point leaves 4 before it. Multi-select picklists are filtered with INCLUDES (or EXCLUDES).
Exercise
Why does a roll-up summary field not work on a lookup relationship, and what can you use in Apex instead?
Show answer
Roll-up summary fields require a master-detail relationship. With a lookup, run an aggregate SOQL query, such as SUM with GROUP BY, to compute the total in code.