In this blog, I am going to show a declarative solution to deduping records when a ‘Get Records’ is set to return all records and we need to select a value, from these records, that appear multiple times.
In Salesforce Flows, we use ‘Get Records’ where we can control the filter conditions of the records we want to return. I ran into a problem where I wanted to be able to select one record/value that was returned for use later in my screen flow but had duplicates to select from and it was not a good user experience. I was able to piece together different tips and tricks across various resources to come up with a solution and this blog puts it all together using only declarative options. Here’s how to Declaratively Remove Duplicates from Get (all) Records.
Solution: Declaratively Remove Duplicates from Get (all) Records.
To do this, we will use elements in the screenshot below.
Sample Use Case
My use case was using Field Service. A Service Resource can log their Time Sheets against multiple Work Orders throughout the day, and their time needs to be associated with each specific Work Order as well as the Time Sheet for that day.
There is a custom screen component for how they enter their time against each Work Order, which is their start and stop time plus a break start and stop. So that is two Time Sheet Entry (TSE) records – one from the start of work to the break start and another from when their break ends to when they stop their work for that Work Order. They do not count break time.
Of course, people can make time entry mistakes, so I wanted to give the user an option to select which Service Resource, for that Work Order, needed to be edited (they work in crews so there are multiple Service Resources per Work Order as well).
No matter how I set the filter conditions in my Get Record, I am always going to return multiple TSE records, and from there I need to select one Service Resource to edit. The TSE records get associated with the Work Order so we count the time that appears just to that Work Order. If I did the Time Sheet only, the most I could narrow it down was a single day, but the Service Resource may work multiple Work Orders in a day.
Create Custom Object
As mentioned above – we need to create a custom object to temporarily store the records. We’ll create the records as part of the loop and delete them when we are done, but we’ll need something to pick from that isn’t the duplicate value.
Name the record whatever you want (I called mine SR (Service Resource) Storage).
- The name can be set to Auto-Number
- Create a lookup field to the Work Order
- Create a lookup field to the Service Resource
- Create a formula(text) field so when your users select a record it is a value they can recognize and not the Salesforce ID values (Optional)
You can create more fields if you need. The most important field to create is the value that we are deduping (which for me was the Service Resource). You will also want to include any values you’ll use to filter on, which was the Work Order in my use case.
Use your ‘Get Record’ and set the object to what needs to be de-duped. I launch my Time Sheet edit screen flow from the Work Order, so that is my ‘recordId’ variable in this example. We are going to edit time only associated with this work order.
Loop your multiple records
Loop through all of your TSEs that you just got. The collection variable will be automatically created if we select ‘All records’ in our Get Records as highlighted above.
Before we create our decision we need to create a text variable that allows multiple text values to be collected.
The decision element is really where the de-duplication happens. For each item in the loop, we will make the decision of does this current item’s Service Resource Id (not the record id) appear in our text variable already? If it does NOT, we will add it and continue in the flow. If it does, we already have what we need so we’ll send it right back to the loop for the next item.
NOTE: You can set this value to whatever your use case is. I am deduping off the Service Resource, but you can compare against whatever you value you need. Because we are using the ‘Contains’ operator be careful with like terms.
First Assignment: Add Service Resource Id to text variable
We continue down the flow, but we need to add the Service Resource Id, the first time it appears in a Get Record, to the text variable we created above. Add the Id to the text variable as seen below.
If a future record is looped and the same value is contained in our text variable, we will not repeat this step. This is where the deduplication happens.
Second Assignment: Set Record Variable Values
Before we create this assignment, we will want to create a record variable. Set the object to the custom object we created above.
Then set the values in an assignment. The specific values you need to set will be based on the custom object setup above. For my use case, I just need the Work Order and Service Resource.
Third Assignment: Add the record variable to the collection
Before we create this assignment, we will need to create a record collection variable. Ensure to check the ‘Allow multiple values (collection)’ checkbox to true.
Now add the record from the step above to the record collection using an assignment.
Create Custom Object records (temporarily)
Finish your loop and after the last item use a ‘Create Records’ element. We will create the records in our collection above.
Service Resource Selection Screen
Now we display all of the Service Resources who logged time against a Work Order and most importantly – just one option shows for each resource!
Before using our screen, we’ll want to create a record choice set:
- Object equals your custom object
- Conditions equal your recordId (for me it was the Work Order)
- Choice label – this is where I used the text formula above when setting up the custom object. The user will see the name of the Service Resource instead of the Salesforce Id
- Store the value of the ID selected (because we will delete these records)
Use a screen element and picklist component, setting your record choice set as the values to pick from.
Delete Temporary Records
Delete the temporary custom object records as part of this flow. Make sure this is a step in the flow you are working on, we don’t need these records for anything else and it will take up storage space. You can delete the records from your collection above.
- What you do with the single selected record is up to you and fits your use case. Through a series of steps, I pre-populated the timesheet edit screen and also deleted the outdated TSEs so when the new TSEs are created, we don’t double count time.
- I may have overlooked some of the best practices related to flows to solve for my specific use case. Happy to hear any feedback or ways you would have done it differently!
We hope these step-by-step instructions on how to declaratively remove duplicates from Get (all) record, in Salesforce flow were helpful! Let us know if you need help with your duplicates or using flow builder. You can learn more about us, we’re an Austin-based Salesforce Consulting partner, with a passion and belief that the Salesforce platform’s capabilities can help businesses run more efficiently and effectively. Thanks for stopping by the Roycon Salesforce blog, be sure to subscribe. Thanks for reading and as always, happy building!
Small Business Consultant
Brian is one of our Senior Salesforce Business Consultants, who continuously strive for customer success and the highest level of satisfaction. He has a sophisticated background in supply chain management, sales, and marketing which makes him one of our finest Salesforce business consultants.