Skip to content

Commit

Permalink
toLabel (#95)
Browse files Browse the repository at this point in the history
* toLabel

* toLabel documentation
  • Loading branch information
pgajek2 authored Nov 10, 2023
1 parent 3acc472 commit a0166f3
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
16 changes: 16 additions & 0 deletions force-app/main/default/classes/SOQL.cls
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public virtual inherited sharing class SOQL implements Queryable {
Queryable sum(SObjectField field, String alias);
// SELECT - GROUPING
Queryable grouping(SObjectField field, String alias);
// SELECT - toLabel
Queryable toLabel(SObjectField field);
Queryable toLabel(String field);
// USING SCOPE
Queryable delegatedScope();
Queryable mineScope();
Expand Down Expand Up @@ -414,6 +417,15 @@ public virtual inherited sharing class SOQL implements Queryable {
return this;
}

public SOQL toLabel(SObjectField field) {
return toLabel(field.getDescribe().getName());
}

public SOQL toLabel(String field) {
builder.fields.toLabel(field);
return this;
}

public SOQL delegatedScope() {
builder.scope.delegated();
return this;
Expand Down Expand Up @@ -862,6 +874,10 @@ public virtual inherited sharing class SOQL implements Queryable {
fields.add(field.getDescribe().getName());
}

public void toLabel(String field) {
fields.add('toLabel(' + field + ')');
}

public void with(String relationshipPath, List<SObjectField> fields) {
for (SObjectField field : fields) {
with(relationshipPath, field);
Expand Down
24 changes: 24 additions & 0 deletions force-app/main/default/classes/SOQL_Test.cls
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,30 @@ private class SOQL_Test {
Assert.areEqual('SELECT LeadSource, Rating, GROUPING(LeadSource) grpLS, GROUPING(Rating) grpRating, COUNT(Name) cnt FROM Lead GROUP BY ROLLUP(LeadSource, Rating)', soql);
}

@IsTest
static void toLabelWithSObjectField() {
// Test
String soql = SOQL.of(Lead.SObjectType)
.with(Lead.Company)
.toLabel(Lead.Status)
.toString();

// Verify
Assert.areEqual('SELECT Company, toLabel(Status) FROM Lead', soql);
}

@IsTest
static void toLabelWithStringField() {
// Test
String soql = SOQL.of(Lead.SObjectType)
.with(Lead.Company)
.toLabel('Recordtype.Name')
.toString();

// Verify
Assert.areEqual('SELECT Company, toLabel(Recordtype.Name) FROM Lead', soql);
}

@IsTest
static void withField() {
// Test
Expand Down
38 changes: 38 additions & 0 deletions website/docs/api/soql.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ The following are methods for `SOQL`.

- [`grouping(SObjectField field, String alias)`](#grouping)

[**toLabel**](#tolabel)

- [`toLabel(SObjectField field)`](#tolabel)
- [`toLabel(String field)`](#tolabel)

[**SUBQUERY**](#sub-query)

- [`with(SubQuery subQuery)`](#with-subquery)
Expand Down Expand Up @@ -687,6 +692,39 @@ SOQL.of(Lead.SObjectType)
.toAggregated();
```

## toLabel

> To translate SOQL query results into the language of the user who submits the query, use the toLabel method.
**Signature**

```apex
toLabel(SObjectField field)
toLabel(String field)
```

**Example**

```sql
SELECT Company, toLabel(Status) FROM Lead
```
```apex
SOQL.of(Lead.SObjectType)
.with(Lead.Company)
.toLabel(Lead.Status)
.toList();
```

```sql
SELECT Company, toLabel(Recordtype.Name) FROM Lead
```
```apex
SOQL.of(Lead.SObjectType)
.with(Lead.Company)
.toLabel('Recordtype.Name')
.toList();
```

## USING SCOPE

[USING SCOPE](https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_using_scope.htm)
Expand Down

0 comments on commit a0166f3

Please sign in to comment.