I'm wondering whether its possible to use a trigger to find the most frequently recurring value in a related list, and then populate a field on the parent object with that value. And if so, what trigger structure might best achieve this?
for example, the list of values in the custom field of related list records might be:
list relatedList = dog, cat, bird, bird, bird, cat, chicken, dog, horse, bird, dog, bird
I want the trigger to iterate through this list, and find the most frequently recurring value. So.... 'dog' occurs 3 times 'cat' occurs 2 times 'bird' occurs 5 times 'chicken' occurs 1 times 'horse' occurs 1 times
So.....'bird' occurs most frequently, therefore I want to populate the parent object field 'frequent_item__c' with 'bird'
I'm thinking the trigger structure might be something like...
//iterate through the list
//for each item in the list, count the number of identical items
//find the item with the highest frequency count
//populate the parent object field with the item
Am I on the right track?? I'm fairly new to Apex development with a few triggers and test classes under my belt, but feel that this one is a bit out of reach. Any assistance appreciated.
Thanks,