Skip to content

Commit

Permalink
with related field
Browse files Browse the repository at this point in the history
  • Loading branch information
pgajek2 committed May 15, 2023
1 parent 3933a16 commit 45585ed
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
6 changes: 6 additions & 0 deletions force-app/main/default/classes/SOQL.cls
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public inherited sharing class SOQL implements Queryable {
SOQL with(Set<SObjectField> fields);
SOQL with(List<SObjectField> fields);
SOQL with(String fields);
SOQL with(String relationshipName, SObjectField field);
SOQL with(String relationshipName, List<SObjectField> fields);
SOQL with(SubQuery subQuery); // SOQL.SubQuery

Expand Down Expand Up @@ -233,6 +234,11 @@ public inherited sharing class SOQL implements Queryable {
return this;
}

public SOQL with(String relationshipName, SObjectField field) {
builder.fields.with(relationshipName, field);
return this;
}

public SOQL with(String relationshipName, List<SObjectField> fields) {
builder.fields.with(relationshipName, fields);
return this;
Expand Down
14 changes: 14 additions & 0 deletions force-app/main/default/classes/SOQLTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ private class SOQLTest {
Assert.areEqual('SELECT Id, Name, BillingCity FROM Account', soql);
}

@IsTest
static void withRelatedField() {
// Test
String soql = SOQL.of(Account.SObjectType)
.with(new List<SObjectField>{
Account.Name, Account.BillingCity
})
.with('CreatedBy', User.Name)
.toString();

// Verify
Assert.areEqual('SELECT Name, BillingCity, CreatedBy.Name FROM Account', soql);
}

@IsTest
static void withRelatedFields() {
// Test
Expand Down
24 changes: 23 additions & 1 deletion website/docs/api/soql.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,33 @@ SOQL.of(Account.SObjectType)
.toList();
```

### with related field

Allows to add parent field to a query.

**Signature**

```apex
SOQL with(String relationshipName, SObjectField field)
```

**Example**

```sql
SELECT CreatedBy.Name
FROM Account
```
```apex
SOQL.of(Account.SObjectType)
.with('CreatedBy', User.Name)
.toList();
```

### with related fields

[SELECT](https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_fields.htm)

Allows to add parent field to a query.
Allows to add parent fields to a query.

**Signature**

Expand Down

1 comment on commit 45585ed

@vercel
Copy link

@vercel vercel bot commented on 45585ed May 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.