-
Notifications
You must be signed in to change notification settings - Fork 584
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1474 from alexed1/ReactiveCollectionActions_1
Reactive collection actions 1
- Loading branch information
Showing
48 changed files
with
1,381 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...eactiveCollectionProcessors/force-app/main/default/classes/FilterCollectionController.cls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* Call an Invocable Action from a Reactive Flow Screen Component | ||
* | ||
* Sample Controller with an AuraEnabled class, that calls an Invocable Flow Action, designed to be called by an LWC that is | ||
* exposed to flow as a reactive Flow Screen Component | ||
* | ||
* Created By: Eric Smith | ||
* | ||
* 10/22/23 Version: 1.0.1 Initial Release | ||
* | ||
* LWC: filterCollection_rsc | ||
* Controller: FilterCollectionController, FilterCollectionControllerTest | ||
* Action: FilterCollection | ||
* Collection Processors (https://unofficialsf.com/list-actions-for-flow/) | ||
* | ||
**/ | ||
|
||
// Code commented this way is a standard part of the template and should stay as is | ||
// * Code commented this way should be adjusted to fit your use case | ||
|
||
// * Give the class a name similar to the invocable action | ||
public with sharing class FilterCollectionController { | ||
|
||
// * Define each of the attributes to be returned by the invocable action and then passed back to the calling LWC | ||
public class ReturnResultsWrapper { | ||
List<SObject> outputCollection; | ||
String errors; | ||
} | ||
|
||
@AuraEnabled | ||
// * Give the method a name similar to the invocable action | ||
public static String filterCollection( | ||
// * Define each of the arguments to be passed into this controller by the LWC and then directly on to the invocable action | ||
List<SObject> inputCollection, | ||
String formula | ||
) { | ||
|
||
// Initialize the return results object | ||
ReturnResultsWrapper curRR = new ReturnResultsWrapper(); | ||
|
||
// * Set the 2nd argument to the name of the Invocable Apex Action | ||
Invocable.Action action = Invocable.Action.createCustomAction('apex', 'FilterCollection'); | ||
|
||
// * For each of the action's input attributes (Request), set the 1st argument to the name of the InvocableVariable | ||
// * and the 2nd argument to the corresponding value passed into this controller | ||
action.setInvocationParameter('inputCollection', inputCollection); | ||
action.setInvocationParameter('formula', formula); | ||
|
||
// Invoke the action | ||
List<Invocable.Action.Result> results = action.invoke(); | ||
|
||
// If a result was returned ... | ||
if (results.size() > 0 && results[0].isSuccess()) { | ||
|
||
// * Assign each of the returned attributes to the corresponding value in the ReturnResultsWrapper | ||
curRR.outputCollection = objToObj(results[0].getOutputParameters().get('outputCollection')); | ||
curRR.errors = objToString(results[0].getOutputParameters().get('errors')); | ||
|
||
} | ||
// Return the results wrapper to the calling LWC | ||
return JSON.serialize(curRR); | ||
|
||
} | ||
|
||
// Convert an object to a list of objects and fix date format | ||
private static List<SObject> objToObj(Object obj) { | ||
return (List<SObject>) JSON.deserialize(JSON.serialize(obj).replace('+0000','Z'), List<SObject>.class); | ||
} | ||
|
||
// Convert an object to a list of strings | ||
// private static List<String> objToList(Object obj) { | ||
// return (List<String>) JSON.deserialize(JSON.serialize(obj), List<String>.class); | ||
// } | ||
|
||
// Convert an object to a String | ||
private static String objToString(Object obj) { | ||
return String.valueof(obj); | ||
} | ||
|
||
// Convert an object to an integer | ||
// private static Integer objToInteger(Object obj) { | ||
// return Integer.valueof(obj); | ||
// } | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
...llectionProcessors/force-app/main/default/classes/FilterCollectionController.cls-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>58.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
27 changes: 27 additions & 0 deletions
27
...iveCollectionProcessors/force-app/main/default/classes/FilterCollectionControllerTest.cls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
@IsTest | ||
public with sharing class FilterCollectionControllerTest { | ||
|
||
@IsTest | ||
static void tryFilterCollection() { | ||
|
||
List<SObject> accounts = new List<SObject>{ | ||
new Account(Name = 'A1', NumberOfEmployees = 10), | ||
new Account(Name = 'A2', NumberOfEmployees = 15), | ||
new Account(Name = 'A3', NumberOfEmployees = 10) | ||
}; | ||
insert accounts; | ||
|
||
String formula = '$Record.NumberOfEmployees=10'; | ||
String result = FilterCollectionController.filterCollection(accounts, formula); | ||
Map<String, Object> resultMap = (Map<String, Object>) JSON.deserializeUntyped(result); | ||
// List<SObject> recs = objToObj(resultMap.get('outputCollection')); | ||
Assert.areEqual( 2, objToObj(resultMap.get('outputCollection')).size() ); | ||
|
||
} | ||
|
||
// Convert an object to a list of objects | ||
private static List<SObject> objToObj(Object obj) { | ||
return (List<SObject>) JSON.deserialize(JSON.serialize(obj), List<SObject>.class); | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
...tionProcessors/force-app/main/default/classes/FilterCollectionControllerTest.cls-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>58.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
95 changes: 95 additions & 0 deletions
95
...tiveCollectionProcessors/force-app/main/default/classes/FindCommonAndUniqueController.cls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* Call an Invocable Action from a Reactive Flow Screen Component | ||
* | ||
* Sample Controller with an AuraEnabled class, that calls an Invocable Flow Action, designed to be called by an LWC that is | ||
* exposed to flow as a reactive Flow Screen Component | ||
* | ||
* Created By: Eric Smith | ||
* | ||
* 10/03/23 Version: 1.0.1 Initial Release | ||
* | ||
* LWC: findCommonAndUnique | ||
* Controller: FindCommonAndUniqueController, FindCommonAndUniqueControllerTest | ||
* Action: FindCommonAndUniqueRecords | ||
* Collection Processors (https://unofficialsf.com/list-actions-for-flow/) | ||
* | ||
**/ | ||
|
||
// Code commented this way is a standard part of the template and should stay as is | ||
// * Code commented this way should be adjusted to fit your use case | ||
|
||
// * Give the class a name similar to the invocable action | ||
public with sharing class FindCommonAndUniqueController { | ||
|
||
// * Define each of the attributes to be returned by the invocable action and then passed back to the calling LWC | ||
public class ReturnResultsWrapper { | ||
List<SObject> sourceUniqueRecordCollection; | ||
List<SObject> sourceCommonRecordCollection; | ||
List<SObject> targetUniqueRecordCollection; | ||
List<SObject> targetCommonRecordCollection; | ||
} | ||
|
||
@AuraEnabled | ||
// * Give the method a name similar to the invocable action | ||
public static String findCommonAndUnique( | ||
// * Define each of the arguments to be passed into this controller by the LWC and then directly on to the invocable action | ||
List<SObject> sourceRecordCollection, | ||
String sourceUniqueID, | ||
List<SObject> targetRecordCollection, | ||
String targetUniqueID, | ||
Boolean emptyListsReturnNull | ||
) { | ||
|
||
// Initialize the return results object | ||
ReturnResultsWrapper curRR = new ReturnResultsWrapper(); | ||
|
||
// * Set the 2nd argument to the name of the Invocable Apex Action | ||
Invocable.Action action = Invocable.Action.createCustomAction('apex', 'FindCommonAndUniqueRecords'); | ||
|
||
// * For each of the action's input attributes (Request), set the 1st argument to the name of the InvocableVariable | ||
// * and the 2nd argument to the corresponding value passed into this controller | ||
action.setInvocationParameter('sourceRecordCollection', sourceRecordCollection); | ||
action.setInvocationParameter('sourceUniqueID', sourceUniqueID); | ||
action.setInvocationParameter('targetRecordCollection', targetRecordCollection); | ||
action.setInvocationParameter('targetUniqueID', targetUniqueID); | ||
action.setInvocationParameter('emptyListsReturnNull', emptyListsReturnNull); | ||
|
||
// Invoke the action | ||
List<Invocable.Action.Result> results = action.invoke(); | ||
|
||
// If a result was returned ... | ||
if (results.size() > 0 && results[0].isSuccess()) { | ||
|
||
// * Assign each of the returned attributes to the corresponding value in the ReturnResultsWrapper | ||
curRR.sourceCommonRecordCollection = objToObj(results[0].getOutputParameters().get('sourceCommonRecordCollection')); | ||
curRR.targetCommonRecordCollection = objToObj(results[0].getOutputParameters().get('targetCommonRecordCollection')); | ||
curRR.sourceUniqueRecordCollection = objToObj(results[0].getOutputParameters().get('sourceUniqueRecordCollection')); | ||
curRR.targetUniqueRecordCollection = objToObj(results[0].getOutputParameters().get('targetUniqueRecordCollection')); | ||
|
||
} | ||
// Return the results wrapper to the calling LWC | ||
return JSON.serialize(curRR); | ||
|
||
} | ||
|
||
// Convert an object to a list of objects and fix date format | ||
private static List<SObject> objToObj(Object obj) { | ||
return (List<SObject>) JSON.deserialize(JSON.serialize(obj).replace('+0000','Z'), List<SObject>.class); | ||
} | ||
|
||
// Convert an object to a list of strings | ||
private static List<String> objToList(Object obj) { | ||
return (List<String>) JSON.deserialize(JSON.serialize(obj), List<String>.class); | ||
} | ||
|
||
// Convert an object to a String | ||
private static String objToString(Object obj) { | ||
return String.valueof(obj); | ||
} | ||
|
||
// Convert an object to an integer | ||
private static Integer objToInteger(Object obj) { | ||
return Integer.valueof(obj); | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
...ctionProcessors/force-app/main/default/classes/FindCommonAndUniqueController.cls-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>58.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
32 changes: 32 additions & 0 deletions
32
...CollectionProcessors/force-app/main/default/classes/FindCommonAndUniqueControllerTest.cls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
@IsTest | ||
public with sharing class FindCommonAndUniqueControllerTest { | ||
|
||
@IsTest | ||
static void tryFindCommonAndUnique() { | ||
|
||
List<SObject> sourceAccounts = new List<SObject>{ | ||
new Account(Name = 'A1', Site = '1'), | ||
new Account(Name = 'A2', Site = '2'), | ||
new Account(Name = 'A3', Site = '3') | ||
}; | ||
|
||
List<SObject> targetAccounts = new List<SObject>{ | ||
new Account(Name = 'B1', Site = '1'), | ||
new Account(Name = 'B4', Site = '4') | ||
}; | ||
|
||
String result = FindCommonAndUniqueController.findCommonAndUnique(sourceAccounts, 'Site', targetAccounts, 'Site', false); | ||
Map<String, Object> resultMap = (Map<String, Object>) JSON.deserializeUntyped(result); | ||
Assert.areEqual( 2, objToObj(resultMap.get('sourceUniqueRecordCollection')).size() ); | ||
Assert.areEqual( 1, objToObj(resultMap.get('sourceCommonRecordCollection')).size() ); | ||
Assert.areEqual( 1, objToObj(resultMap.get('targetUniqueRecordCollection')).size() ); | ||
Assert.areEqual( 1, objToObj(resultMap.get('targetCommonRecordCollection')).size() ); | ||
|
||
} | ||
|
||
// Convert an object to a list of objects | ||
private static List<SObject> objToObj(Object obj) { | ||
return (List<SObject>) JSON.deserialize(JSON.serialize(obj), List<SObject>.class); | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
...nProcessors/force-app/main/default/classes/FindCommonAndUniqueControllerTest.cls-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>58.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
Oops, something went wrong.