From 45585ed3a62b033b68b63cbea09fbfdbb9d847f7 Mon Sep 17 00:00:00 2001 From: Piotr PG Gajek Date: Mon, 15 May 2023 16:49:14 +0200 Subject: [PATCH] with related field --- force-app/main/default/classes/SOQL.cls | 6 ++++++ force-app/main/default/classes/SOQLTest.cls | 14 ++++++++++++ website/docs/api/soql.md | 24 ++++++++++++++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/force-app/main/default/classes/SOQL.cls b/force-app/main/default/classes/SOQL.cls index d1943f3d..48461d2a 100644 --- a/force-app/main/default/classes/SOQL.cls +++ b/force-app/main/default/classes/SOQL.cls @@ -49,6 +49,7 @@ public inherited sharing class SOQL implements Queryable { SOQL with(Set fields); SOQL with(List fields); SOQL with(String fields); + SOQL with(String relationshipName, SObjectField field); SOQL with(String relationshipName, List fields); SOQL with(SubQuery subQuery); // SOQL.SubQuery @@ -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 fields) { builder.fields.with(relationshipName, fields); return this; diff --git a/force-app/main/default/classes/SOQLTest.cls b/force-app/main/default/classes/SOQLTest.cls index 59b1647e..3a690032 100644 --- a/force-app/main/default/classes/SOQLTest.cls +++ b/force-app/main/default/classes/SOQLTest.cls @@ -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{ + 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 diff --git a/website/docs/api/soql.md b/website/docs/api/soql.md index 7d2f858c..46c1da7f 100644 --- a/website/docs/api/soql.md +++ b/website/docs/api/soql.md @@ -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**