|
| 1 | +--- |
| 2 | +sidebar_position: 70 |
| 3 | +--- |
| 4 | + |
| 5 | +# HavingFilterGroup |
| 6 | + |
| 7 | +Create group of having conditions. |
| 8 | + |
| 9 | +```apex |
| 10 | +SOQL.of(Lead.SObjectType) |
| 11 | + .with(Lead.LeadSource) |
| 12 | + .count(Lead.Name) |
| 13 | + .groupBy(Lead.City) |
| 14 | + .groupBy(Lead.LeadSource) |
| 15 | + .have(SOQL.HavingFilterGroup |
| 16 | + .add(SOQL.HavingFilter.count(Lead.Name).greaterThan(100)) |
| 17 | + .add(SOQL.HavingFilter.with(Lead.City).startsWith('San')) |
| 18 | + ) |
| 19 | + .toAggregated(); |
| 20 | +``` |
| 21 | + |
| 22 | + |
| 23 | +## Methods |
| 24 | + |
| 25 | +The following are methods for `HavingFilterGroup`. |
| 26 | + |
| 27 | +[**ADD CONDITION**](#add-condition) |
| 28 | + |
| 29 | +- [`add(HavingFilterGroup havingFilterGroup)`](#add) |
| 30 | +- [`add(HavingFilter havingFilter)`](#add) |
| 31 | +- [`add(String dynamicCondition)`](#add) |
| 32 | + |
| 33 | +[**ORDER**](#order) |
| 34 | + |
| 35 | +- [`anyConditionMatching()`](#anyconditionmatching) |
| 36 | +- [`conditionLogic(String order)`](#conditionlogic) |
| 37 | + |
| 38 | +## ADD CONDITION |
| 39 | +### add |
| 40 | + |
| 41 | +Allows to add multiple conditions. |
| 42 | +Add a [`SOQL.HavingFilter`](soql-having-filter.md) or [`SOQL.HavingFilterGroup`](soql-having-filter-group.md) or `String`. |
| 43 | + |
| 44 | +**Signature** |
| 45 | + |
| 46 | +```apex |
| 47 | +HavingFilterGroup add(HavingFilterGroup havingFilterGroup) |
| 48 | +HavingFilterGroup add(HavingFilter havingFilter) |
| 49 | +HavingFilterGroup add(String dynamicCondition) |
| 50 | +``` |
| 51 | + |
| 52 | +**Example** |
| 53 | + |
| 54 | +```sql |
| 55 | +SELECT LeadSource, COUNT(Name) |
| 56 | +FROM Lead |
| 57 | +GROUP BY LeadSource, City |
| 58 | +HAVING (COUNT(Name) > 100 AND City LIKE 'San%') |
| 59 | +``` |
| 60 | + |
| 61 | +```apex |
| 62 | +// build conditions on fly |
| 63 | +SOQL.HavingFilterGroup havingFilterGroup = SOQL.HavingFilterGroup |
| 64 | + .add(SOQL.HavingFilter.count(Lead.Name).greaterThan(100)) |
| 65 | + .add(SOQL.HavingFilter.with(Lead.City).startsWith('San')); |
| 66 | +
|
| 67 | +SOQL.of(Lead.SObjectType) |
| 68 | + .with(Lead.LeadSource) |
| 69 | + .count(Lead.Name) |
| 70 | + .groupBy(Lead.LeadSource) |
| 71 | + .groupBy(Lead.City) |
| 72 | + .have(havingFilterGroup) |
| 73 | + .toAggregated(); |
| 74 | +``` |
| 75 | + |
| 76 | +```apex |
| 77 | +SOQL.of(Lead.SObjectType) |
| 78 | + .with(Lead.LeadSource) |
| 79 | + .count(Lead.Name) |
| 80 | + .groupBy(Lead.LeadSource) |
| 81 | + .groupBy(Lead.City) |
| 82 | + .have(SOQL.HavingFilterGroup |
| 83 | + .add(SOQL.HavingFilter.count(Lead.Name).greaterThan(100)) |
| 84 | + .add(SOQL.HavingFilter.with(Lead.City).startsWith('San')) |
| 85 | + ).toAggregated(); |
| 86 | +``` |
| 87 | + |
| 88 | +```apex |
| 89 | +SOQL.of(Lead.SObjectType) |
| 90 | + .with(Lead.LeadSource) |
| 91 | + .count(Lead.Name) |
| 92 | + .groupBy(Lead.LeadSource) |
| 93 | + .have('(COUNT(Name) > 100 AND COUNT(Name) < 200)') |
| 94 | + .have(SOQL.HavingFilterGroup |
| 95 | + .add(SOQL.HavingFilter.count(Lead.Name).greaterThan(400)) |
| 96 | + .add(SOQL.HavingFilter.count(Lead.Name).lessThan(500)) |
| 97 | + ).toAggregated(); |
| 98 | +``` |
| 99 | + |
| 100 | +## ORDER |
| 101 | +### conditionLogic |
| 102 | + |
| 103 | +Set conditions order for SOQL query. |
| 104 | +When not specify all conditions will be with `AND`. |
| 105 | + |
| 106 | +**Signature** |
| 107 | + |
| 108 | +```apex |
| 109 | +HavingFilterGroup conditionLogic(String order) |
| 110 | +``` |
| 111 | + |
| 112 | +**Example** |
| 113 | + |
| 114 | +```sql |
| 115 | +SELECT LeadSource, COUNT(Name) |
| 116 | +FROM Lead |
| 117 | +GROUP BY LeadSource, City |
| 118 | +HAVING (COUNT(Name) > 100 OR City LIKE 'San%') |
| 119 | +``` |
| 120 | +```apex |
| 121 | +SOQL.of(Lead.SObjectType) |
| 122 | + .with(Lead.LeadSource) |
| 123 | + .count(Lead.Name) |
| 124 | + .groupBy(Lead.LeadSource) |
| 125 | + .groupBy(Lead.City) |
| 126 | + .have(SOQL.HavingFilterGroup |
| 127 | + .add(SOQL.HavingFilter.count(Lead.Name).greaterThan(100)) |
| 128 | + .add(SOQL.HavingFilter.with(Lead.City).startsWith('San')) |
| 129 | + .conditionLogic('1 OR 2') |
| 130 | + ).toAggregated(); |
| 131 | +``` |
| 132 | + |
| 133 | +### anyConditionMatching |
| 134 | + |
| 135 | +When the [conditionLogic](#conditionlogic) is not specified, all conditions are joined using the `AND` operator by default. |
| 136 | + |
| 137 | +To change the default condition logic, you can utilize the `anyConditionMatching` method, which joins conditions using the `OR` operator. |
| 138 | + |
| 139 | +**Signature** |
| 140 | + |
| 141 | +```apex |
| 142 | +HavingFilterGroup anyConditionMatching() |
| 143 | +``` |
| 144 | + |
| 145 | +**Example** |
| 146 | + |
| 147 | +```sql |
| 148 | +SELECT LeadSource, COUNT(Name) |
| 149 | +FROM Lead |
| 150 | +GROUP BY LeadSource, City |
| 151 | +HAVING (COUNT(Name) > 100 OR City LIKE 'San%') |
| 152 | +``` |
| 153 | +```apex |
| 154 | +SOQL.of(Lead.SObjectType) |
| 155 | + .with(Lead.LeadSource) |
| 156 | + .count(Lead.Name) |
| 157 | + .groupBy(Lead.LeadSource) |
| 158 | + .groupBy(Lead.City) |
| 159 | + .have(SOQL.HavingFilterGroup |
| 160 | + .add(SOQL.HavingFilter.count(Lead.Name).greaterThan(100)) |
| 161 | + .add(SOQL.HavingFilter.with(Lead.City).startsWith('San')) |
| 162 | + .anyConditionMatching() |
| 163 | + ).toAggregated(); |
| 164 | +``` |
0 commit comments