I want to create a unique account(unique account name) collection variable in Salesforce flow using the get record element, we have a duplicate account name(multiple accounts with the same name) and I want to skip the same name account in my collection variable.
1 Answer
Creating Unique collections requires using advanced programming concepts like using Sets
and Maps
. These are not today supported out-of-box in Flows.
There is an idea for this on IdeaExchange. Consider upvoting!
However, the community has done some open source work that you can adopt and experiment with.
There is a component here that you can install as an unmanaged package.
The apex code for this is pretty simple and open-sourced as well and easy to understand if you are familiar with apex.
global with sharing class DedupeRecordCollection {
@InvocableMethod
global static List<FlowResponse> dedupe(List<FlowRequest> requests) {
List<FlowResponse> flowResponses = new List<FlowResponse>();
for (FlowRequest request : requests) {
FlowResponse flowResponse = new FlowResponse();
flowResponse.outputRecordCollection = getUniqueSObjectCollection(request);
flowResponses.add(flowResponse);
}
return flowResponses;
}
private static List<SObject> getUniqueSObjectCollection(FlowRequest request) {
Map<String, SObject> sobjectMap = new Map<String, SObject>();
for (SObject record : request.inputRecordCollection) {
try {
String fieldValue = String.valueOf(record.get(request.fieldToDedupeOn));
if(!sobjectMap.containsKey(fieldValue)) {
sobjectMap.put(fieldValue, record);
}
} catch(SObjectException e) {
System.debug(e.getMessage());
}
}
return sobjectMap.values();
}
global class FlowRequest {
@InvocableVariable(required=true)
global List<SObject> inputRecordCollection;
@InvocableVariable(required=true)
global String fieldToDedupeOn;
}
global class FlowResponse {
public FlowResponse() {
outputRecordCollection = new List<SObject>();
}
@InvocableVariable
global List<SObject> outputRecordCollection;
}
}