I have a helper class in which I'm trying to create a new record using information from various other related records that are gathered using maps and lists.
My problem is that I'm trying to build a list that only pulls one record from each related object. Here is the structure of my problem:
Locations: Parent to Inventory control and Receipt objects, both Lookup relationships
IC: Object that I'm trying to build a list of. Child record of location, can be multiple per location.
Receipt: Object that trigger fires on, and Child record of location.
I've put all my Receipt ID's into a set along with the Location ID indicated on that Receipt record. I am building my IC list using the following Query:
SELECT ID from IC WHERE Location__c IN :RecIds.keyset(); (Location__c is my key in the RecIds Map (Id of Location, Id of Receipt). Everything works just fine if I only have one IC record per location, but if I have more than one, than I have a problem.
I want to only return one IC record per Location in my list. So if I had 20 Receipts in my trigger, 10 different locations indicated on those Receipts and each location had 2 IC records associated with it, I would only want to populate my list with 10 IC records, essentially 1 per location.
I cannot limit my query to a certain number of records because that won't eliminate the correct IC records from the query.
I don't think I can use an AggregateResult list with this since I don't really have anything to aggregate? Not sure what the best approach would be so I figured I'd ask the experts!
Thanks in advance,