Skip to content

Commit

Permalink
includes and excludes (#49)
Browse files Browse the repository at this point in the history
* includes and excludes

* refactoring

* enable acr in scratch

* added scratch org support for ACR and person accounts

---------

Co-authored-by: Maciej Ptak <[email protected]>
  • Loading branch information
pgajek2 and 0ptaq0 authored Jun 28, 2023
1 parent 1de6eef commit 07116cf
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 1 deletion.
9 changes: 8 additions & 1 deletion config/project-scratch-def.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
{
"orgName": "Beyond The Cloud Dev",
"edition": "Developer",
"features": ["EnableSetPasswordInApi"],
"features": [
"EnableSetPasswordInApi",
"ContactsToMultipleAccounts",
"PersonAccounts"
],
"settings": {
"lightningExperienceSettings": {
"enableS1DesktopEnabled": true
},
"mobileSettings": {
"enableS1EncryptedStoragePref2": false
},
"accountSettings": {
"enableRelateContactToMultipleAccounts": true
}
}
}
28 changes: 28 additions & 0 deletions force-app/main/default/classes/SOQL.cls
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ public inherited sharing class SOQL implements Queryable {
Filter notIn(Iterable<Object> iterable); // NOT IN :inList or inSet
Filter notIn(List<Object> inList); // NOT IN :inList
Filter notIn(InnerJoin joinQuery); // SOQL.InnerJoin
Filter includesAll(Iterable<String> values); // join with ;
Filter includesSome(Iterable<String> values); // join with ,
Filter excludesAll(Iterable<String> values); // join with ,
Filter excludesSome(Iterable<String> values); // join with ;

Filter removeWhenNull(); // Condition will be removed for value = null

Expand Down Expand Up @@ -1114,6 +1118,30 @@ public inherited sharing class SOQL implements Queryable {
return set('NOT IN', joinQuery);
}

public Filter includesAll(Iterable<String> iterable) {
//Bind expressions can't be used with other clauses, such as INCLUDES.
skipBinding = true;
return set('INCLUDES', '(\'' + String.join(iterable, ';') + '\')');
}

public Filter includesSome(Iterable<String> iterable) {
//Bind expressions can't be used with other clauses, such as INCLUDES.
skipBinding = true;
return set('INCLUDES', '(\'' + String.join(iterable, '\', \'') + '\')');
}

public Filter excludesAll(Iterable<String> iterable) {
//Bind expressions can't be used with other clauses, such as EXCLUDES.
skipBinding = true;
return set('EXCLUDES', '(\'' + String.join(iterable, '\', \'') + '\')');
}

public Filter excludesSome(Iterable<String> iterable) {
//Bind expressions can't be used with other clauses, such as EXCLUDES.
skipBinding = true;
return set('EXCLUDES', '(\'' + String.join(iterable, ';') + '\')');
}

private Filter set(String comperator, Object value) {
this.value = value;
this.comperator = comperator;
Expand Down
60 changes: 60 additions & 0 deletions force-app/main/default/classes/SOQL_Test.cls
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,66 @@ private class SOQL_Test {
Assert.areEqual(names, binding.get('v1'));
}

@IsTest
static void inlcudesAll() {
// Setup
List<String> roles = new List<String>{ 'Business User', 'Decision Maker' };

// Test
SOQL builder = SOQL.of(AccountContactRelation.SObjectType)
.with(AccountContactRelation.Id)
.whereAre(SOQL.Filter.with(AccountContactRelation.Roles).includesAll(roles));

// Verify
String soql = builder.toString();
Assert.areEqual('SELECT Id FROM AccountContactRelation WHERE Roles INCLUDES (\'Business User;Decision Maker\')', soql);
}

@IsTest
static void inlcudesSome() {
// Setup
List<String> roles = new List<String>{ 'Executive Sponsor', 'Economic Decision Maker' };

// Test
SOQL builder = SOQL.of(AccountContactRelation.SObjectType)
.with(AccountContactRelation.Id)
.whereAre(SOQL.Filter.with(AccountContactRelation.Roles).includesSome(roles));

// Verify
String soql = builder.toString();
Assert.areEqual('SELECT Id FROM AccountContactRelation WHERE Roles INCLUDES (\'Executive Sponsor\', \'Economic Decision Maker\')', soql);
}

@IsTest
static void excludesAll() {
// Setup
List<String> roles = new List<String>{ 'Technical Buyer', 'Economic Buyer' };

// Test
SOQL builder = SOQL.of(AccountContactRelation.SObjectType)
.with(AccountContactRelation.Id)
.whereAre(SOQL.Filter.with(AccountContactRelation.Roles).excludesAll(roles));

// Verify
String soql = builder.toString();
Assert.areEqual('SELECT Id FROM AccountContactRelation WHERE Roles EXCLUDES (\'Technical Buyer\', \'Economic Buyer\')', soql);
}

@IsTest
static void excludesSome() {
// Setup
List<String> roles = new List<String>{ 'Evaluator', 'Influencer' };

// Test
SOQL builder = SOQL.of(AccountContactRelation.SObjectType)
.with(AccountContactRelation.Id)
.whereAre(SOQL.Filter.with(AccountContactRelation.Roles).excludesSome(roles));

// Verify
String soql = builder.toString();
Assert.areEqual('SELECT Id FROM AccountContactRelation WHERE Roles EXCLUDES (\'Evaluator;Influencer\')', soql);
}

@IsTest
static void isNull() {
// Test
Expand Down
62 changes: 62 additions & 0 deletions website/docs/api/soql-filter.md
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,68 @@ SOQL.of(Contact.SObjectType)
)).toList();
```

## multi-select picklist

### includesAll

```sql
SELECT Id
FROM AccountContactRelation
WHERE Roles INCLUDES ('Business User;Decision Maker')
```
```apex
List<String> roles = new List<String>{ 'Business User', 'Decision Maker' };
SOQL builder = SOQL.of(AccountContactRelation.SObjectType)
.with(AccountContactRelation.Id)
.whereAre(SOQL.Filter.with(AccountContactRelation.Roles).includesAll(roles));
```

### includesSome

```sql
SELECT Id
FROM AccountContactRelation
WHERE Roles INCLUDES ('Business User', 'Decision Maker')
```
```apex
List<String> roles = new List<String>{ 'Business User', 'Decision Maker' };
SOQL builder = SOQL.of(AccountContactRelation.SObjectType)
.with(AccountContactRelation.Id)
.whereAre(SOQL.Filter.with(AccountContactRelation.Roles).includesSome(roles));
```

### excludesAll

```sql
SELECT Id
FROM AccountContactRelation
WHERE Roles EXCLUDES ('Business User', 'Decision Maker')
```
```apex
List<String> roles = new List<String>{ 'Business User', 'Decision Maker' };
SOQL builder = SOQL.of(AccountContactRelation.SObjectType)
.with(AccountContactRelation.Id)
.whereAre(SOQL.Filter.with(AccountContactRelation.Roles).excludesAll(roles));
```

### excludesSome

```sql
SELECT Id
FROM AccountContactRelation
WHERE Roles EXCLUDES ('Business User;Decision Maker')
```
```apex
List<String> roles = new List<String>{ 'Business User', 'Decision Maker' };
SOQL builder = SOQL.of(AccountContactRelation.SObjectType)
.with(AccountContactRelation.Id)
.whereAre(SOQL.Filter.with(AccountContactRelation.Roles).excludesSome(roles));
```

## additional

### removeWhenNull
Expand Down

1 comment on commit 07116cf

@vercel
Copy link

@vercel vercel bot commented on 07116cf Jun 28, 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.