Salesforce Winter ’27: Compare Fields in SOQL with FORMULA()
Salesforce developers often face a simple problem: you need to filter records based on a value calculated from two fields.
Traditionally, you might create a formula field, query the records, or perform the calculation inside Apex after retrieving the data.
Salesforce is introducing another option: FORMULA() in the SOQL WHERE clause.
This capability allows arithmetic calculations between supported fields directly during query filtering. Salesforce currently documents it as a beta capability, subject to the Beta Services Terms.
What is FORMULA() in SOQL?
FORMULA() allows you to perform supported arithmetic calculations inside a SOQL WHERE clause.
The basic pattern is:
WHERE FORMULA('FieldA - FieldB') > value
For example:
SELECT Id, Name, Revenue__c, Cost__c
FROM Order__c
WHERE FORMULA('Revenue__c - Cost__c') > 250
This query finds orders where:
Revenue - Cost > 250
Instead of retrieving a larger set of records and performing the calculation in Apex, the calculated condition becomes part of the query itself. Salesforce’s developer documentation and engineering blog describe this as useful for reducing unnecessary formula fields and post-query filtering.
Why is this useful?
Before FORMULA(), developers commonly had three options.
Option 1 — Create a formula field
For example:
Profit__c
with:
Revenue__c - Cost__c
Then query:
SELECT Id, Name
FROM Order__c
WHERE Profit__c > 250
This works, but it changes the Salesforce data model.
Option 2 — Query and calculate in Apex
You could query the records first:
List<Order__c> orders = [
SELECT Id, Revenue__c, Cost__c
FROM Order__c
];
for (Order__c order : orders) {
Decimal profit = order.Revenue__c - order.Cost__c;
if (profit > 250) {
// Process order
}
}
The problem is that Salesforce has already returned records that you ultimately don’t need.
Option 3 — Calculate inside SOQL
With FORMULA():
SELECT Id, Name, Revenue__c, Cost__c
FROM Order__c
WHERE FORMULA('Revenue__c - Cost__c') > 250
Now the calculation becomes part of the filtering condition.
This can make the intent much clearer:
Query → Calculate → Filter
instead of:
Query everything → Apex calculation → Filter
Salesforce specifically highlights the ability to keep calculated filtering logic close to the data-access layer.
A practical example
Imagine an Order__c object containing:
| Field | Type |
|---|---|
Revenue__c | Currency |
Cost__c | Currency |
OrderDate__c | Date |
ShipDate__c | Date |
Suppose we want to find orders where profit is greater than $250.
We can write:
SELECT Id,
Name,
Revenue__c,
Cost__c
FROM Order__c
WHERE FORMULA('Revenue__c - Cost__c') > 250
The calculation happens as part of the query’s filtering logic.
What data types are supported?
According to the current Salesforce documentation, the supported data types include:
Double / DecimalIntegerCurrencyDateDateTime
There are also restrictions around mixing date and datetime values.
