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
Greg EnderleGreg Enderle 

Update ranking dynamically based on field value

I have a custom object "Quota", and am attempting to update a custom field "Rank" based on the ranking of quota records via the value in the field "Amount over Quota".  For example, the quota record with the largest value in "Amount over Quota" would need to have the "Rank" set at 1, and so on.  

I am struggling mightily with writing a trigger.  Anyone willing to lend a hand?  I have a total of 4 rankings that need to be updated in this way, but if I can get the first one done I'm sure I can figure out how to get the others done as well.

Thanks!
Richard Jimenez 9Richard Jimenez 9
Hi Greg,

You will need to create triggers on the Quota object for after insert, update and delete to maintain the rank value.

Example:

List<Quota__c> quotas = [SELECT Id FROM Quota__c ORDER BY Amount_Over_Quota__c DESC]
Integer rank = 1;
for(Quota__c quota : quotas)
{
   quota.Rank__c = rank;
   rank++;
}
update quotas;

Depending on the record volumes you may want to consider async apex to maintain the ranks.

Hope that gives you something to work on,
Richard.
Greg Enderle 9Greg Enderle 9
Thanks for the help Richard!  I will give it a go.
Andrew Wilson 45Andrew Wilson 45
Greg - was this successful? I need to something similar and can't figure out where to even begin. To make it more complicated, I'm looking to rank records related to another record. Example - Rank the number of contracts (custom objects) related to a vendor (custom object).