Skip to main content

HavingFilterGroup

Create group of having conditions.

SOQL.of(Lead.SObjectType)
.with(Lead.LeadSource)
.count(Lead.Name)
.groupBy(Lead.City)
.groupBy(Lead.LeadSource)
.have(SOQL.HavingFilterGroup
.add(SOQL.HavingFilter.count(Lead.Name).greaterThan(100))
.add(SOQL.HavingFilter.with(Lead.City).startsWith('San'))
)
.toAggregated();

Methods

The following are methods for HavingFilterGroup.

ADD CONDITION

ORDER

ADD CONDITION

add

Allows to add multiple conditions. Add a SOQL.HavingFilter or SOQL.HavingFilterGroup or String.

Signature

HavingFilterGroup add(HavingFilterGroup havingFilterGroup)
HavingFilterGroup add(HavingFilter havingFilter)
HavingFilterGroup add(String dynamicCondition)

Example

SELECT LeadSource, COUNT(Name)
FROM Lead
GROUP BY LeadSource, City
HAVING (COUNT(Name) > 100 AND City LIKE 'San%')
// build conditions on fly
SOQL.HavingFilterGroup havingFilterGroup = SOQL.HavingFilterGroup
.add(SOQL.HavingFilter.count(Lead.Name).greaterThan(100))
.add(SOQL.HavingFilter.with(Lead.City).startsWith('San'));

SOQL.of(Lead.SObjectType)
.with(Lead.LeadSource)
.count(Lead.Name)
.groupBy(Lead.LeadSource)
.groupBy(Lead.City)
.have(havingFilterGroup)
.toAggregated();
SOQL.of(Lead.SObjectType)
.with(Lead.LeadSource)
.count(Lead.Name)
.groupBy(Lead.LeadSource)
.groupBy(Lead.City)
.have(SOQL.HavingFilterGroup
.add(SOQL.HavingFilter.count(Lead.Name).greaterThan(100))
.add(SOQL.HavingFilter.with(Lead.City).startsWith('San'))
).toAggregated();
SOQL.of(Lead.SObjectType)
.with(Lead.LeadSource)
.count(Lead.Name)
.groupBy(Lead.LeadSource)
.have('(COUNT(Name) > 100 AND COUNT(Name) < 200)')
.have(SOQL.HavingFilterGroup
.add(SOQL.HavingFilter.count(Lead.Name).greaterThan(400))
.add(SOQL.HavingFilter.count(Lead.Name).lessThan(500))
).toAggregated();

ORDER

conditionLogic

Set conditions order for SOQL query. When not specify all conditions will be with AND.

Signature

HavingFilterGroup conditionLogic(String order)

Example

SELECT LeadSource, COUNT(Name)
FROM Lead
GROUP BY LeadSource, City
HAVING (COUNT(Name) > 100 OR City LIKE 'San%')
SOQL.of(Lead.SObjectType)
.with(Lead.LeadSource)
.count(Lead.Name)
.groupBy(Lead.LeadSource)
.groupBy(Lead.City)
.have(SOQL.HavingFilterGroup
.add(SOQL.HavingFilter.count(Lead.Name).greaterThan(100))
.add(SOQL.HavingFilter.with(Lead.City).startsWith('San'))
.conditionLogic('1 OR 2')
).toAggregated();

anyConditionMatching

When the conditionLogic is not specified, all conditions are joined using the AND operator by default.

To change the default condition logic, you can utilize the anyConditionMatching method, which joins conditions using the OR operator.

Signature

HavingFilterGroup anyConditionMatching()

Example

SELECT LeadSource, COUNT(Name)
FROM Lead
GROUP BY LeadSource, City
HAVING (COUNT(Name) > 100 OR City LIKE 'San%')
SOQL.of(Lead.SObjectType)
.with(Lead.LeadSource)
.count(Lead.Name)
.groupBy(Lead.LeadSource)
.groupBy(Lead.City)
.have(SOQL.HavingFilterGroup
.add(SOQL.HavingFilter.count(Lead.Name).greaterThan(100))
.add(SOQL.HavingFilter.with(Lead.City).startsWith('San'))
.anyConditionMatching()
).toAggregated();