Skip to content

Commit

Permalink
Merge pull request #1474 from alexed1/ReactiveCollectionActions_1
Browse files Browse the repository at this point in the history
Reactive collection actions 1
  • Loading branch information
alexed1 authored Nov 14, 2023
2 parents 0b7ab22 + 3fdd809 commit 1b75c1b
Show file tree
Hide file tree
Showing 48 changed files with 1,381 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
/**
* Call an Invocable Action from a Reactive Flow Screen Component
*
* By: Eric Smith
* Date: 07/24/23
* Version: 1.0.0
* 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
*
* 07/24/23 Version: 1.0.0 Initial Release
* 10/15/23 Version: 1.0.1 Updated with Date format fix
*
* LWC: collectionCalculate
* Controller: CollectionCalculateController, CollectionCalculateControllerTest
Expand Down Expand Up @@ -62,10 +67,15 @@ public with sharing class CollectionCalculateController {

}

// 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);
// }
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
* 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
*
* By: Eric Smith
* Date: 07/21/23
* Version: 1.0.0
* Created By: Eric Smith
*
* 07/21/23 Version: 1.0.0 Initial Release
* 10/15/23 Version: 1.0.1 Updated with date format fix
*
* LWC: extractFieldToCollection
* Controller: ExtractFieldToCollectionController, ExtractFieldToCollectionControllerTest
Expand Down Expand Up @@ -52,6 +53,10 @@ public with sharing class ExtractFieldToCollectionController {

}

private static List<SObject> objToObj(Object obj) { // Convert an object to a list of objects and fix date format
return (List<SObject>) JSON.deserialize(JSON.serialize(obj).replace('+0000','Z'), List<SObject>.class);
}

private static List<String> objToList(Object obj) { // Convert an object to a list of strings
return (List<String>) JSON.deserialize(JSON.serialize(obj), List<String>.class);
}
Expand All @@ -60,4 +65,8 @@ public with sharing class ExtractFieldToCollectionController {
return String.valueof(obj);
}

private static Integer objToInteger(Object obj) { // Convert an object to an integer
return Integer.valueof(obj);
}

}
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);
// }

}
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>
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);
}

}
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>
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);
}

}
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>
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);
}

}
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>
Loading

0 comments on commit 1b75c1b

Please sign in to comment.