Skip to content

Commit

Permalink
Merge pull request #1492 from alexed1/ColProc_ConvertStringToCol
Browse files Browse the repository at this point in the history
Col proc convert string to col
  • Loading branch information
alexed1 authored Dec 14, 2023
2 parents 4053694 + d096623 commit 88fa181
Show file tree
Hide file tree
Showing 16 changed files with 353 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
public with sharing class ConvertCSVToStringCollection {
global with sharing class ConvertCSVToStringCollection {

@invocableMethod(label='Convert Comma-separated-values to String Collection'
description='Converts a Comma-separated-values string (eg, from a field) to a String Collection'
category= 'Utilities')
public static List<Response> execute (List<Request> requests) {
global static List<Response> execute (List<Request> requests) {

List<Response> responseList = new List<Response>();
for (Request curRequest : requests) {
Expand All @@ -19,17 +19,17 @@ public with sharing class ConvertCSVToStringCollection {
return responseList;
}

public class Request {
global class Request {
@invocableVariable(label='Input String' description='Input' required=true)
public String csvString;
global String csvString;

@invocableVariable(label='Delimiter' description='Delimiter string; defaults to comma (,)' required=false)
public String delimiter;
global String delimiter;
}

public class Response {
global class Response {
@invocableVariable(label='String Collection' description='Collection variable to store output')
public List<String> stringCollection;
global List<String> stringCollection;

}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
public with sharing class ConvertStringCollectionToCSV {
global with sharing class ConvertStringCollectionToCSV {

@invocableMethod(label='Convert String Collection to Comma-separated-values'
description='Converts a String Collection to Comma-separated-values string'
category= 'Utilities')
public static List<Response> execute (List<Request> requests) {
global static List<Response> execute (List<Request> requests) {

List<Response> responseList = new List<Response>();
for (Request curRequest : requests) {
Expand All @@ -21,17 +21,17 @@ public with sharing class ConvertStringCollectionToCSV {
return responseList;
}

public class Request {
global class Request {
@invocableVariable(label='String Collection' description='Input' required=true)
public List<String> stringCollection;
global List<String> stringCollection;

@invocableVariable(label='Delimiter' description='Delimiter string; defaults to comma (,)' required=false)
public String delimiter;
global String delimiter;
}

public class Response {
global class Response {
@invocableVariable(label='String' description='String variable to store output')
public String csvString;
global String csvString;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@echo off
if "%1" neq "" set packageName=%1
@echo on
sfdx package:create -v lexhost --name "%packageName%" --path force-app --package-type Unlocked
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@echo off
if "%1" neq "" set version=%1
@echo on
sfdx package:version:create -v lexhost -w 20 -x -c -n %version%.0 -d force-app\
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@echo off
if "%1" neq "" set packageName=%1
if "%2" neq "" set version=%2
@echo on
sfdx package:version:report -v lexhost --package "%packageName%@%version%"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@echo off
if "%1" neq "" set packageName=%1
if "%2" neq "" set version=%2
@echo on
sfdx package:version:promote -v lexhost --package "%packageName%@%version%"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@echo off
if "%1" neq "" goto skipprompt
set /p packageName="Set Package Name: "
goto exit
:skipprompt
set packageName=%1
:exit
echo Package Name: %packageName%
@echo on
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@echo off
if "%1" neq "" goto skipprompt
set /p version="Set Version Number: "
goto exit
:skipprompt
set version=%1
:exit
echo Version: %version%
@echo on
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@echo off
echo Package Name: %packageName%
echo Version: %version%
@echo on
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* 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
*
* 12/12/23 Version: 1.0.2 Initial Release
*
* LWC: convertCSVStringCollection_rsc
* Controller: ConvertCSVStringCollectionController, ConvertCSVStringCollectionControllerTest
* Actions: ConvertCSVToStringCollection, ConvertStringCollectionToCSV
* Original: Convert Strings to String Collections, and Vice Versa (https://unofficialsf.com/new-flow-actions-to-convert-csv-strings-to-string-collections-and-vice-versa/)
* Requires v1.3 or later
**/

// 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 ConvertCSVStringCollectionController {

// * Define each of the attributes to be returned by the invocable action and then passed back to the calling LWC
public class ReturnResultsWrapper {
List<String> stringCollectionOut; // String to Collection
String csvStringOut; // Collection to String
}

@AuraEnabled
// * Give the method a name similar to the invocable action
public static String convertCSVCollection(
// * Define each of the arguments to be passed into this controller by the LWC and then directly on to the invocable action
String csvStringIn, // String to Collection
List<String> stringCollectionIn, // Collection to String
String delimiter // Used for both
) {

// Initialize the return results object
ReturnResultsWrapper curRR = new ReturnResultsWrapper();

// * Set the 2nd argument to the name of the Invocable Apex Action
// String to Collection
Invocable.Action action1 = Invocable.Action.createCustomAction('apex', 'ConvertCSVToStringCollection');
// Collection to String
Invocable.Action action2 = Invocable.Action.createCustomAction('apex', 'ConvertStringCollectionToCSV');

// * 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
// String to Collection
action1.setInvocationParameter('delimiter', delimiter);
action1.setInvocationParameter('csvString', csvStringIn);
// Collection to String
action2.setInvocationParameter('delimiter', delimiter);
action2.setInvocationParameter('stringCollection', stringCollectionIn);

// Invoke the action
List<Invocable.Action.Result> results1 = action1.invoke(); // String to Collection
List<Invocable.Action.Result> results2 = action2.invoke(); // Collection to String

// * Clear prior results
curRR.stringCollectionOut = New List<String>();
curRR.csvStringOut = '';

// If a result was returned ...
if (results1.size() > 0 && results1[0].isSuccess()) { // String to Collection
// * Assign each of the returned attributes to the corresponding value in the ReturnResultsWrapper
curRR.stringCollectionOut = objToList(results1[0].getOutputParameters().get('stringCollection'));
}
if (results2.size() > 0 && results2[0].isSuccess()) { // Collection to String
// * Assign each of the returned attributes to the corresponding value in the ReturnResultsWrapper
curRR.csvStringOut = objToString(results2[0].getOutputParameters().get('csvString'));
}

// 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>59.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@isTest
public with sharing class ConvertCSVStringCollectionControllerTest {

@isTest
static void testConvert() {

List<String> textCollection = new List<String>{};
String csvString = 'abc;def';

String result = ConvertCSVStringCollectionController.convertCSVCollection(csvString, textCollection, ';');
Map<String, Object> resultMap = (Map<String, Object>) JSON.deserializeUntyped(result);
List<String> objAsStrings = (List<String>) JSON.deserialize(JSON.serialize( resultMap.get('stringCollectionOut') ), List<String>.class);
Assert.areEqual( objAsStrings[0], 'abc' );
Assert.areEqual( objAsStrings[1], 'def' );

result = ConvertCSVStringCollectionController.convertCSVCollection('', objAsStrings, ';');
resultMap = (Map<String, Object>) JSON.deserializeUntyped(result);
Assert.areEqual( csvString, resultMap.get('csvStringOut') );

}

}
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,12 @@
<template>

<!-- This template includes a standard 'error' output attribute that will be exposed on the flow screen -->
<template if:true={error}>
<!-- * insert the name of the action in the error message-->
The Convert CSV String Collection screen component on this screen is reporting the following Error: {error}
</template>

<!-- Track, but do not display the reactive attribute(s) -->
<span hidden><lightning-formatted-text value={reactiveValue} onchange={handleOnChange}></lightning-formatted-text></span>

</template>
Loading

0 comments on commit 88fa181

Please sign in to comment.