You need to sign in to do that
Don't have an account?

Writing a trigger to sort a concatenated formula field by numerical value
Greetings, I am looking to drafta a concatenated formula field that would sort values in descending order, where the highest value referencing field appears first and so on and so forth. I have my code designed as such, but am stuck with how to reference the fields. Are there any steps I can take or ideas I should consider. Hope it helps.
trigger ProductDonationsSummary on Product_Donations__c (before insert) { Map<Integer, String> mapNumValueAndFieldLabel = new Map<Integer,String>(); String strMessage; //null checkers for the field and adding the number value as key and the string message as the value if its not null/blank/zero IF(Total_LettersTally__c != null && Total_LettersTally__c != '' && Integer.valueOf(Total_LettersTally__c) != 0) mapNumValueAndFieldLabel.put(Integer.valueOf(Total_LettersTally__c), Total_LettersTally__c + " letters"); IF(Gators__c != null && Gators__c != '' && Integer.valueOf(Gators__c) != 0) mapNumValueAndFieldLabel.put(Integer.valueOf(Gators__c), Gators__c + " Gators"); IF(Paracords__c != null && Paracords__c != '' && Integer.valueOf(Paracords__c) != 0) mapNumValueAndFieldLabel.put(Integer.valueOf(Paracords__c), Paracords__c + " paracord bracelets"); //and so on for other fields.... //Proceed further only if either of the fields have non zero values if(mapNumValueAndFieldLabel.values().size() > 0) { List<Integer> lstNumValue = new List<integer> (); lstNumValue.addAll(mapNumValueAndFieldLabel.keyset()); //Sort the Values (by default SFDC sorts it in ascending order) lstNumValue.sort(); //Iterating through the sorted Integer list in reverse order (descending) to get the final String message from the map for(Integer i = lstNumValue.size()-1; i >= 0; i--) { strMessage = mapNumValueAndFieldLabel.get(lstNumValue(i)) + ", "; } } //strMessage will hold the message you want to display }
Try this. if Total_LettersTally__c, Gators__c and Paracords__c fields are on Product_Donations__c object then below code would help you.
The only thing I noticed here is you are storing all integer values as key in one map (mapNumValueAndFieldLabel) and map keys are always unique so if get duplicates then map will automatically remove duplicate and you would lose that key and its value.
I hope this would help.
The only issue is that I get duplicate variable I (attempt to recreate the variable with type integer) error on line 32, otherwise, almost there. I will research and provide a result if I find one.