Compare & Contrast Two Record Collections with ‘FindCommonAndUncommonRecords’
Introduction
Do you hate food recipe blogs that go on and on about a special story about how they grew up smelling grandma’s cooking and getting frustrated because you just want to get the recipe? Your kids are crying, the garlic and onions are burning, and you finally get to the recipe but it’s covered by an ad on your phone and the browser crashes.
This is not that post. Let’s get right to it!
I recently wrapped up work on a nifty action called ‘FindCommonAndUncommonRecords’ that can compare two like/unlike record collections based on a unique identifier that you specify. It then can provide 4 outputs providing records unique and shared between the two collections based on the identifying fields you provide.
The thoughts and solutions in this post are my own and not that of Salesforce.
Scenario
The action can take a bit of effort to wrap your head around so I created a scenario showing off a hypothetical model involving Accounts and two custom objects, Gas Stations and Gas Station Relationships. An Account can be related to many Gas Stations through the Gas Station Relationships custom object.
Let’s say you build a screen flow that lets the user associate whatever Gas Station they want to the Account (using Datatable or a Lookup component). You will be creating these Gas Station Relationship records to set up that relationship. The catch here is that there are already Gas Stations tied to the account, but you have no way of filtering those out of the datatable/Lookup easily.
The user picks 4 gas stations to associate to Blackbeards Grog Emporium:
- Station 17
- Station 55
- Station 23
- Station 3
We then run this new action to get the Gas Stations not tied to the Account, or in other words the records unique to the selected collection by the user.
Here are the Inputs:
- sourceRecordCollection – gasStationRelationshipsTiedToAccount
- sourceUniqueID – Related_Gas_Station__c
- targetRecordCollection – gasStationsSelectedFromDatatable
- targetUniqueID – Id
Here’s the output:
targetUniqueCollection (Gas_Station__c) – The gas stations unique to the selected gas stations compared to the account’s related gas stations. Returned as Gas Station records.
- Station 55
- Station 23
- Station 3
targetCommonCollection (Gas_Station__c) – The gas stations shared between the account and the selected gas stations. Returned as Gas Station records.
- Station 17
sourceUniqueCollection (Gas_Station_Relationship__c) – The gas stations unique to the account compared to the selected gas stations. Returned as Gas Station Relationship records.
- Station 45
- Station 66
sourceCommonCollection (Gas_Station_Relationship__c) – The gas stations shared between the account and the selected gas stations. Returned as Gas Station Relationship records.
- Station 17
From here, you can safely loop over the ‘targetUniqueCollection’ (the unique records selected by the user) to build your junction records without dupes!
Tutorial Video
Inputs
- sourceRecordCollection – The first collection you want to compare
- Type – Record Collection
- sourceUniqueID – The field API name / identifier you want to use to find records in the ‘target’ collection (Case Sensitive)
- Type – Text
- targetRecordCollection – The the second collection you want to compare
- Type – Record Collection
- targetUniqueID – The field API name / identifier you want to use to find records in the ‘source’ collection (Case Sensitive)
- Type – Text
Outputs
- sourceUniqueCollection – The unique records in the source collection when compared against the target collection
- Type – Record Collection
- sourceCommonCollection – The shared records between the two collections, returned as the specified source Output sObject type.
- Type – Record Collection
- targetUniqueCollection – The unique records in the target collection when compared against the source collection
- Type – Record Collection
- targetCommonCollection – The shared records between the two collections, returned as the specified target Output sObject type.
- Type – Record Collection
Install
This is part of Collection Actions package, version 1.23 and higher.
Source
https://github.com/alexed1/LightningFlowComponents/blob/master/flow_action_components/CollectionProcessors/force-app/main/default/classes/FindCommonAndUniqueRecords.cls
https://github.com/alexed1/LightningFlowComponents/blob/master/flow_action_components/CollectionProcessors/force-app/main/default/classes/FindCommonAndUniqueRecordsTest.cls