Skip to content

Commit bfc3f6c

Browse files
committed
dynamic sort
Signed-off-by: Piotr PG Gajek <[email protected]>
1 parent 5b7bc20 commit bfc3f6c

File tree

4 files changed

+71
-26
lines changed

4 files changed

+71
-26
lines changed

force-app/main/default/classes/main/standard-soql/SOQL.cls

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public virtual inherited sharing class SOQL implements Queryable {
106106
Queryable orderBy(String field, String direction);
107107
Queryable orderBy(String relationshipName, SObjectField field);
108108
Queryable sortDesc();
109+
Queryable sort(String direction);
109110
Queryable nullsLast();
110111
// LIMIT
111112
Queryable setLimit(Integer amount);
@@ -169,12 +170,12 @@ public virtual inherited sharing class SOQL implements Queryable {
169170
// WHERE
170171
SubQuery whereAre(FilterGroup filterGroup);
171172
SubQuery whereAre(Filter filter);
172-
//ORDER BY
173+
// ORDER BY
173174
SubQuery orderBy(SObjectField field);
174175
SubQuery orderBy(String field);
175-
SubQuery orderBy(String field, String direction);
176176
SubQuery orderBy(String relationshipName, SObjectField field);
177177
SubQuery sortDesc();
178+
SubQuery sort(String direction);
178179
SubQuery nullsLast();
179180
// LIMIT
180181
SubQuery setLimit(Integer amount);
@@ -724,6 +725,11 @@ public virtual inherited sharing class SOQL implements Queryable {
724725
return this;
725726
}
726727

728+
public Queryable sort(String direction) {
729+
builder.latestOrderBy.sortingOrder(direction);
730+
return this;
731+
}
732+
727733
public SOQL nullsLast() {
728734
builder.latestOrderBy.nullsLast();
729735
return this;
@@ -1254,13 +1260,13 @@ public virtual inherited sharing class SOQL implements Queryable {
12541260
return this;
12551261
}
12561262

1257-
public SubQuery orderBy(String field, String direction) {
1258-
builder.orderBys.newOrderBy().with(field).sortingOrder(direction);
1263+
public SubQuery sortDesc() {
1264+
builder.latestOrderBy.sortDesc();
12591265
return this;
12601266
}
12611267

1262-
public SubQuery sortDesc() {
1263-
builder.latestOrderBy.sortDesc();
1268+
public SubQuery sort(String direction) {
1269+
builder.latestOrderBy.sortingOrder(direction);
12641270
return this;
12651271
}
12661272

@@ -2095,11 +2101,11 @@ public virtual inherited sharing class SOQL implements Queryable {
20952101
}
20962102

20972103
public void sortingOrder(String direction) {
2098-
sortingOrder = direction;
2104+
this.sortingOrder = direction;
20992105
}
21002106

21012107
public void nullsLast() {
2102-
nullsOrder = 'LAST';
2108+
this.nullsOrder = 'LAST';
21032109
}
21042110

21052111
public override String toString() {

force-app/main/default/classes/main/standard-soql/SOQL_Test.cls

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,8 @@ private class SOQL_Test {
877877
.with(Account.Name)
878878
.with(SOQL.SubQuery.of('Contacts')
879879
.with(Contact.Id, Contact.Name)
880-
.orderBy('Name', 'ASC')
880+
.orderBy('Name')
881+
.sort('ASC')
881882
.nullsLast()
882883
).toString();
883884

@@ -2850,6 +2851,18 @@ private class SOQL_Test {
28502851
Assert.areEqual('SELECT Id FROM Account ORDER BY Industry ASC NULLS FIRST', soql, 'The generated SOQL should match the expected one.');
28512852
}
28522853

2854+
@IsTest
2855+
static void orderByDynamicWithSort() {
2856+
// Test
2857+
String soql = SOQL.of(Account.SObjectType)
2858+
.orderBy('Industry')
2859+
.sort('ASC')
2860+
.toString();
2861+
2862+
// Verify
2863+
Assert.areEqual('SELECT Id FROM Account ORDER BY Industry ASC NULLS FIRST', soql, 'The generated SOQL should match the expected one.');
2864+
}
2865+
28532866
@IsTest
28542867
static void orderBy() {
28552868
// Test
@@ -2873,7 +2886,6 @@ private class SOQL_Test {
28732886
Assert.areEqual('SELECT Id FROM Contact ORDER BY Account.Name ASC NULLS FIRST', soql, 'The generated SOQL should match the expected one.');
28742887
}
28752888

2876-
28772889
@IsTest
28782890
static void setLimit() {
28792891
// Test

website/docs/api/standard-soql/soql-sub.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ The following are methods for `SubQuery`.
4646

4747
- [`orderBy(SObjectField field)`](#order-by)
4848
- [`orderBy(String field)`](#order-by-string-fields)
49-
- [`orderBy(String field, String direction)`](#orderby-string-fields-with-direction)
5049
- [`orderBy(String relationshipName, SObjectField field)`](#orderby-related)
5150
- [`sortDesc()`](#sortdesc)
51+
- [`sort(String direction)`](#sort)
5252
- [`nullsLast()`](#nullslast)
5353

5454
[**LIMIT**](#limit)
@@ -325,12 +325,14 @@ SOQL.of(Account.SObjectType)
325325
.toList();
326326
```
327327

328-
### orderBy string field with direction
328+
### orderBy related
329+
330+
Order SOQL query by parent field.
329331

330332
**Signature**
331333

332334
```apex
333-
SubQuery orderBy(String field, String direction)
335+
SubQuery orderBy(String relationshipName, SObjectField field)
334336
```
335337

336338
**Example**
@@ -339,25 +341,25 @@ SubQuery orderBy(String field, String direction)
339341
SELECT Id, (
340342
SELECT Id
341343
FROM Contacts
342-
ORDER BY Name DESC
344+
ORDER BY CreatedBy.Name
343345
) FROM Account
344346
```
345347
```apex
346348
SOQL.of(Account.SObjectType)
347349
.with(SOQL.SubQuery.of('Contacts')
348-
.orderBy('Name', 'DESC')
350+
.orderBy('CreatedBy', User.Name)
349351
)
350352
.toList();
351353
```
352354

353-
### orderBy related
355+
### sortDesc
354356

355-
Order SOQL query by parent field.
357+
Default order is ascending (`ASC`).
356358

357359
**Signature**
358360

359361
```apex
360-
SubQuery orderBy(String relationshipName, SObjectField field)
362+
SubQuery sortDesc()
361363
```
362364

363365
**Example**
@@ -366,25 +368,24 @@ SubQuery orderBy(String relationshipName, SObjectField field)
366368
SELECT Id, (
367369
SELECT Id
368370
FROM Contacts
369-
ORDER BY CreatedBy.Name
371+
ORDER BY Name DESC
370372
) FROM Account
371373
```
372374
```apex
373375
SOQL.of(Account.SObjectType)
374376
.with(SOQL.SubQuery.of('Contacts')
375-
.orderBy('CreatedBy', User.Name)
377+
.orderBy(Contact.Name)
378+
.sortDesc()
376379
)
377380
.toList();
378381
```
379382

380-
### sortDesc
381-
382-
Default order is ascending (`ASC`).
383+
### sort
383384

384385
**Signature**
385386

386387
```apex
387-
SubQuery sortDesc()
388+
SubQuery sort(String direction)
388389
```
389390

390391
**Example**
@@ -399,8 +400,8 @@ SELECT Id, (
399400
```apex
400401
SOQL.of(Account.SObjectType)
401402
.with(SOQL.SubQuery.of('Contacts')
402-
.orderBy(Contact.Name)
403-
.sortDesc()
403+
.orderBy('Name')
404+
.sort('DESC')
404405
)
405406
.toList();
406407
```

website/docs/api/standard-soql/soql.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ The following are methods for using `SOQL`:
130130
- [`orderBy(String field, String direction)`](#order-by)
131131
- [`orderBy(String relationshipName, SObjectField field)`](#orderby-related)
132132
- [`sordDesc()`](#sortdesc)
133+
- [`sort(String direction)`](#sort)
133134
- [`nullsLast()`](#nullslast)
134135

135136
[**LIMIT**](#limit)
@@ -1674,6 +1675,31 @@ SOQL.of(Account.SObjectType)
16741675
.toList();
16751676
```
16761677

1678+
### sort
1679+
1680+
1681+
### sort
1682+
1683+
**Signature**
1684+
1685+
```apex
1686+
Queryable sort(String direction)
1687+
```
1688+
1689+
**Example**
1690+
1691+
```sql
1692+
SELECT Id
1693+
FROM Account
1694+
ORDER BY Industry ASC NULLS FIRST
1695+
```
1696+
```apex
1697+
SOQL.of(Account.SObjectType)
1698+
.orderBy('Industry')
1699+
.sort('ASC')
1700+
.toList();
1701+
```
1702+
16771703
### nullsLast
16781704

16791705
By default, null values are sorted first (`NULLS FIRST`).

0 commit comments

Comments
 (0)