function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Clement GeorgeClement George 

Identify most recent 20 records, calculate average of lowest 8

Hi all:

I am trying to build a handicapping system for my clay target club. The requirement is to average the best 8 scores of everyone's last 20. There are 10 different games. 

The custom objects I've built are:

shooter__c - the individual's name + associated data
games__c - a listing of the 10 different games
rounds__c - looks up shooter and games, plus a field for their score. Diff__c is a formula field that subtracts their score from the perfect score.

For each individual, for each game, I would like to average the diff__c on the rounds__c object. I am currently doing this by manually flagging the most recent 20 and the subset of the best 8 and then filtering on those flags = y on my report.

I have two flags on the rounds__c object:
Most_recent_record__c which should be flagged if it is one of the twently most recent records as determined by the date__c field
Lowest_8__c which should be flagged if that score is one of the lowest 8 from the most recent 20. 

I'm not married to Apex updating those flags if there is another way, but I thought a trigger might be the best way about this. 

Thank you in advance!
Suraj Tripathi 47Suraj Tripathi 47
Hi Clement George,
You can use the Query like
 "SELECT id FROM rounds__c order by CreatedDate DESC LIMIT 20" this query will help you in finding the Latest 20 record or
 if you need the Last 20 record of each individual then you can use aggregate result query like
 "SELECT id,shooter__c.Name FROM rounds__c ORDER BY CreatedDate DESC GROUP BY shooter__c. Name"
 then after getting the latest record you can calculate the average of the required field.
In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 
Thanks and Regards
Suraj Tripathi
Dinesh Singh 62Dinesh Singh 62
You can use the Query like
 "SELECT id FROM rounds__c order by CreatedDate DESC LIMIT 20" this query will help you in finding the Latest 20 record or
 if you need the Last 20 record of each individual then you can use aggregate result query like
 "SELECT id,shooter__c.Name FROM rounds__c ORDER BY CreatedDate DESC GROUP BY shooter__c. Name"
 then after getting the latest record you can calculate the average of the required field.
In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 
Thanks and Regards - Hindi Me (https://www.niodemy.com/)
Annette BordeauxAnnette Bordeaux
I have been dealing with so many games on daily basis. As I am a software developer and testing new games. So, I just came to know about the best casino gambling game as https://playcasinoscanada.com/local/saskatchewan-online-casinos/ Which could be played from anywhere in the world. You can try for more with a minimum deposit.