Skip to content

QRY_Function

Class · Group: Query Infrastructure

apex
global inherited sharing class QRY_Function

Typed SOQL function expressions for use in the query builder's SELECT, GROUP BY, WHERE and ORDER BY clauses: the date-part functions (CALENDAR_MONTH, DAY_IN_MONTH, FISCAL_QUARTER, ...) and the geolocation DISTANCE function. One static factory per function; the returned value is an immutable carrier that resolves its wrapped field name once and renders to a finished SOQL expression, so the builder can project, group, filter and sort by the same expression from a single source without hand-spelling raw SOQL. Use the factories with the matching QRY_Builder.Builder overloads: addField, groupBy and orderBy to bucket records by a date part for reporting, and condition (WHERE) together with the geolocation distance factories for proximity filtering and nearest-first ordering.

Example

apex
List<QRY_Builder.AggregateRow> rows = QRY_Builder.selectFrom(Opportunity.SObjectType)
    .addField(QRY_Function.calendarMonth(Opportunity.CloseDate), 'closeMonth')
    .count('Id')
    .groupBy(QRY_Function.calendarMonth(Opportunity.CloseDate))
    .orderBy(QRY_Function.calendarMonth(Opportunity.CloseDate))
    .toAggregateList();
for(QRY_Builder.AggregateRow row : rows)
{
    Integer month = row.getInteger('closeMonth');
    Integer total = row.getInteger('count_Id');
}

See Also: QRY_Builder


Methods

MethodDescription
global static QRY_Function calendarMonth(SObjectField field)Wraps a date/datetime field in CALENDAR_MONTH (1–12 in the calendar year).
global static QRY_Function calendarQuarter(SObjectField field)Wraps a date/datetime field in CALENDAR_QUARTER (1–4 in the calendar year).
global static QRY_Function calendarYear(SObjectField field)Wraps a date/datetime field in CALENDAR_YEAR (the four-digit calendar year).
global static QRY_Function dayInMonth(SObjectField field)Wraps a date/datetime field in DAY_IN_MONTH (1–31).
global static QRY_Function dayInWeek(SObjectField field)Wraps a date/datetime field in DAY_IN_WEEK (1 for Sunday through 7 for Saturday).
global static QRY_Function dayInYear(SObjectField field)Wraps a date/datetime field in DAY_IN_YEAR (1–366).
global static QRY_Function dayOnly(SObjectField field)Wraps a datetime field in DAY_ONLY, returning the date with the time component dropped.
global static QRY_Function distanceInKilometers(SObjectField field, Decimal latitude, Decimal longitude)Wraps a geolocation or compound address field in the SOQL DISTANCE function, measuring the straight-line distance in kilometres from each record to a fixed reference point.
global static QRY_Function distanceInKilometers(SObjectField field, System.Location location)Wraps a geolocation or compound address field in the SOQL DISTANCE function, measuring the straight-line distance in kilometres to a System.Location reference point.
global static QRY_Function distanceInMiles(SObjectField field, Decimal latitude, Decimal longitude)Wraps a geolocation or compound address field in the SOQL DISTANCE function, measuring the straight-line distance in statute miles from each record to a fixed reference point.
global static QRY_Function distanceInMiles(SObjectField field, System.Location location)Wraps a geolocation or compound address field in the SOQL DISTANCE function, measuring the straight-line distance in statute miles to a System.Location reference point.
global static QRY_Function fiscalMonth(SObjectField field)Wraps a date/datetime field in FISCAL_MONTH (1–12 within the org's fiscal year).
global static QRY_Function fiscalQuarter(SObjectField field)Wraps a date/datetime field in FISCAL_QUARTER (1–4 within the org's fiscal year).
global static QRY_Function fiscalYear(SObjectField field)Wraps a date/datetime field in FISCAL_YEAR (the org's fiscal year).
global static QRY_Function hourInDay(SObjectField field)Wraps a datetime field in HOUR_IN_DAY (0–23, in the running user's time zone).
global static QRY_Function weekInMonth(SObjectField field)Wraps a date/datetime field in WEEK_IN_MONTH (1–6).
global static QRY_Function weekInYear(SObjectField field)Wraps a date/datetime field in WEEK_IN_YEAR (1–53).

calendarMonth

apex
global static QRY_Function calendarMonth(SObjectField field)

Wraps a date/datetime field in CALENDAR_MONTH (1–12 in the calendar year).

Parameters

ParameterTypeDescription
fieldSObjectFieldThe date or datetime field to bucket.

Returns QRY_Function — A QRY_Function carrying CALENDAR_MONTH(field) for use in SELECT, GROUP BY or ORDER BY.

Example

apex
.groupBy(QRY_Function.calendarMonth(Opportunity.CloseDate))

calendarQuarter

apex
global static QRY_Function calendarQuarter(SObjectField field)

Wraps a date/datetime field in CALENDAR_QUARTER (1–4 in the calendar year).

Parameters

ParameterTypeDescription
fieldSObjectFieldThe date or datetime field to bucket.

Returns QRY_Function — A QRY_Function carrying CALENDAR_QUARTER(field) for use in SELECT, GROUP BY or ORDER BY.

Example

apex
.groupBy(QRY_Function.calendarQuarter(Opportunity.CloseDate))

calendarYear

apex
global static QRY_Function calendarYear(SObjectField field)

Wraps a date/datetime field in CALENDAR_YEAR (the four-digit calendar year).

Parameters

ParameterTypeDescription
fieldSObjectFieldThe date or datetime field to bucket.

Returns QRY_Function — A QRY_Function carrying CALENDAR_YEAR(field) for use in SELECT, GROUP BY or ORDER BY.

Example

apex
.groupBy(QRY_Function.calendarYear(Opportunity.CloseDate))

dayInMonth

apex
global static QRY_Function dayInMonth(SObjectField field)

Wraps a date/datetime field in DAY_IN_MONTH (1–31).

Parameters

ParameterTypeDescription
fieldSObjectFieldThe date or datetime field to bucket.

Returns QRY_Function — A QRY_Function carrying DAY_IN_MONTH(field) for use in SELECT, GROUP BY or ORDER BY.

Example

apex
.groupBy(QRY_Function.dayInMonth(Opportunity.CloseDate))

dayInWeek

apex
global static QRY_Function dayInWeek(SObjectField field)

Wraps a date/datetime field in DAY_IN_WEEK (1 for Sunday through 7 for Saturday).

Parameters

ParameterTypeDescription
fieldSObjectFieldThe date or datetime field to bucket.

Returns QRY_Function — A QRY_Function carrying DAY_IN_WEEK(field) for use in SELECT, GROUP BY or ORDER BY.

Example

apex
.groupBy(QRY_Function.dayInWeek(Opportunity.CreatedDate))

dayInYear

apex
global static QRY_Function dayInYear(SObjectField field)

Wraps a date/datetime field in DAY_IN_YEAR (1–366).

Parameters

ParameterTypeDescription
fieldSObjectFieldThe date or datetime field to bucket.

Returns QRY_Function — A QRY_Function carrying DAY_IN_YEAR(field) for use in SELECT, GROUP BY or ORDER BY.

Example

apex
.groupBy(QRY_Function.dayInYear(Opportunity.CloseDate))

dayOnly

apex
global static QRY_Function dayOnly(SObjectField field)

Wraps a datetime field in DAY_ONLY, returning the date with the time component dropped. Read back with AggregateRow.getDate(alias), not getInteger — DAY_ONLY yields a Date, unlike the other date functions which yield an Integer date part.

Parameters

ParameterTypeDescription
fieldSObjectFieldThe datetime field to truncate to a date.

Returns QRY_Function — A QRY_Function carrying DAY_ONLY(field) for use in SELECT, GROUP BY or ORDER BY.

Example

apex
.addField(QRY_Function.dayOnly(Opportunity.CreatedDate), 'createdOn')
// ... row.getDate('createdOn')

distanceInKilometers

apex
global static QRY_Function distanceInKilometers(SObjectField field, Decimal latitude, Decimal longitude)

Wraps a geolocation or compound address field in the SOQL DISTANCE function, measuring the straight-line distance in kilometres from each record to a fixed reference point. Use it for proximity filtering and nearest-first ordering (field service, store locator, dispatch). Renders DISTANCE(field, GEOLOCATION(latitude,longitude), 'km'). Place it on the WHERE side with QRY_Builder.Builder.condition(QRY_Function) and on the sort side with the existing orderBy(QRY_Function) overload (ascending is nearest-first).

Parameters

ParameterTypeDescription
fieldSObjectFieldThe geolocation (or compound address) field to measure from.
latitudeDecimalThe latitude of the reference point, in degrees.
longitudeDecimalThe longitude of the reference point, in degrees.

Returns QRY_Function — A QRY_Function carrying DISTANCE(field, GEOLOCATION(latitude,longitude), 'km').

Example

apex
List<Account> nearby = QRY_Builder.selectFrom(Account.SObjectType)
    .condition(QRY_Function.distanceInKilometers(Account.BillingAddress, 37.775, -122.418)).lessThan(16)
    .orderBy(QRY_Function.distanceInKilometers(Account.BillingAddress, 37.775, -122.418)).ascending()
    .toList();
apex
global static QRY_Function distanceInKilometers(SObjectField field, System.Location location)

Wraps a geolocation or compound address field in the SOQL DISTANCE function, measuring the straight-line distance in kilometres to a System.Location reference point. Convenience overload of distanceInKilometers(SObjectField, Decimal, Decimal) for callers that already hold a Location (for example a user's current position). Renders the identical DISTANCE(field, GEOLOCATION(latitude,longitude), 'km') form.

Parameters

ParameterTypeDescription
fieldSObjectFieldThe geolocation (or compound address) field to measure from.
locationSystem.LocationThe reference point to measure distance to.

Returns QRY_Function — A QRY_Function carrying DISTANCE(field, GEOLOCATION(latitude,longitude), 'km').

Example

apex
System.Location origin = System.Location.newInstance(37.775, -122.418);
QRY_Function nearest = QRY_Function.distanceInKilometers(Account.BillingAddress, origin);

distanceInMiles

apex
global static QRY_Function distanceInMiles(SObjectField field, Decimal latitude, Decimal longitude)

Wraps a geolocation or compound address field in the SOQL DISTANCE function, measuring the straight-line distance in statute miles from each record to a fixed reference point. Use it for proximity filtering and nearest-first ordering (field service, store locator, dispatch). Renders DISTANCE(field, GEOLOCATION(latitude,longitude), 'mi'). Place it on the WHERE side with QRY_Builder.Builder.condition(QRY_Function) and on the sort side with the existing orderBy(QRY_Function) overload (ascending is nearest-first).

Parameters

ParameterTypeDescription
fieldSObjectFieldThe geolocation (or compound address) field to measure from.
latitudeDecimalThe latitude of the reference point, in degrees.
longitudeDecimalThe longitude of the reference point, in degrees.

Returns QRY_Function — A QRY_Function carrying DISTANCE(field, GEOLOCATION(latitude,longitude), 'mi').

Example

apex
List<Account> nearby = QRY_Builder.selectFrom(Account.SObjectType)
    .condition(QRY_Function.distanceInMiles(Account.BillingAddress, 37.775, -122.418)).lessThan(10)
    .orderBy(QRY_Function.distanceInMiles(Account.BillingAddress, 37.775, -122.418)).ascending()
    .toList();
apex
global static QRY_Function distanceInMiles(SObjectField field, System.Location location)

Wraps a geolocation or compound address field in the SOQL DISTANCE function, measuring the straight-line distance in statute miles to a System.Location reference point. Convenience overload of distanceInMiles(SObjectField, Decimal, Decimal) for callers that already hold a Location (for example a user's current position). Renders the identical DISTANCE(field, GEOLOCATION(latitude,longitude), 'mi') form.

Parameters

ParameterTypeDescription
fieldSObjectFieldThe geolocation (or compound address) field to measure from.
locationSystem.LocationThe reference point to measure distance to.

Returns QRY_Function — A QRY_Function carrying DISTANCE(field, GEOLOCATION(latitude,longitude), 'mi').

Example

apex
System.Location origin = System.Location.newInstance(37.775, -122.418);
QRY_Function nearest = QRY_Function.distanceInMiles(Account.BillingAddress, origin);

fiscalMonth

apex
global static QRY_Function fiscalMonth(SObjectField field)

Wraps a date/datetime field in FISCAL_MONTH (1–12 within the org's fiscal year).

Parameters

ParameterTypeDescription
fieldSObjectFieldThe date or datetime field to bucket.

Returns QRY_Function — A QRY_Function carrying FISCAL_MONTH(field) for use in SELECT, GROUP BY or ORDER BY.

Example

apex
.groupBy(QRY_Function.fiscalMonth(Opportunity.CloseDate))

fiscalQuarter

apex
global static QRY_Function fiscalQuarter(SObjectField field)

Wraps a date/datetime field in FISCAL_QUARTER (1–4 within the org's fiscal year).

Parameters

ParameterTypeDescription
fieldSObjectFieldThe date or datetime field to bucket.

Returns QRY_Function — A QRY_Function carrying FISCAL_QUARTER(field) for use in SELECT, GROUP BY or ORDER BY.

Example

apex
.groupBy(QRY_Function.fiscalQuarter(Opportunity.CloseDate))

fiscalYear

apex
global static QRY_Function fiscalYear(SObjectField field)

Wraps a date/datetime field in FISCAL_YEAR (the org's fiscal year).

Parameters

ParameterTypeDescription
fieldSObjectFieldThe date or datetime field to bucket.

Returns QRY_Function — A QRY_Function carrying FISCAL_YEAR(field) for use in SELECT, GROUP BY or ORDER BY.

Example

apex
.groupBy(QRY_Function.fiscalYear(Opportunity.CloseDate))

hourInDay

apex
global static QRY_Function hourInDay(SObjectField field)

Wraps a datetime field in HOUR_IN_DAY (0–23, in the running user's time zone).

Parameters

ParameterTypeDescription
fieldSObjectFieldThe datetime field to bucket.

Returns QRY_Function — A QRY_Function carrying HOUR_IN_DAY(field) for use in SELECT, GROUP BY or ORDER BY.

Example

apex
.groupBy(QRY_Function.hourInDay(Opportunity.CreatedDate))

weekInMonth

apex
global static QRY_Function weekInMonth(SObjectField field)

Wraps a date/datetime field in WEEK_IN_MONTH (1–6).

Parameters

ParameterTypeDescription
fieldSObjectFieldThe date or datetime field to bucket.

Returns QRY_Function — A QRY_Function carrying WEEK_IN_MONTH(field) for use in SELECT, GROUP BY or ORDER BY.

Example

apex
.groupBy(QRY_Function.weekInMonth(Opportunity.CloseDate))

weekInYear

apex
global static QRY_Function weekInYear(SObjectField field)

Wraps a date/datetime field in WEEK_IN_YEAR (1–53).

Parameters

ParameterTypeDescription
fieldSObjectFieldThe date or datetime field to bucket.

Returns QRY_Function — A QRY_Function carrying WEEK_IN_YEAR(field) for use in SELECT, GROUP BY or ORDER BY.

Example

apex
.groupBy(QRY_Function.weekInYear(Opportunity.CloseDate))