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
Medhanie HabteMedhanie Habte 

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

}



 
Ketankumar PatelKetankumar Patel
Hi Medhanie,

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. 
trigger ProductDonationsSummary on Product_Donations__c (before insert) 
{  
Map<Integer, String> mapNumValueAndFieldLabel = new Map<Integer,String>();
for(Product_Donations__c pd : Trigger.new){
//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(pd.Total_LettersTally__c != null && pd.Total_LettersTally__c != '' && pd.Integer.valueOf(Total_LettersTally__c) != 0)
{
    mapNumValueAndFieldLabel.put(Integer.valueOf(pd.Total_LettersTally__c), pd.Total_LettersTally__c + ' letters');
}

IF(pd.Gators__c != null && pd.Gators__c != '' && Integer.valueOf(pd.Gators__c) != 0)
{
    mapNumValueAndFieldLabel.put(Integer.valueOf(pd.Gators__c), pd.Gators__c + ' Gators');
}

IF(pd.Paracords__c != null && pd.Paracords__c != '' && Integer.valueOf(pd.Paracords__c) != 0)
{
    mapNumValueAndFieldLabel.put(Integer.valueOf(pd.Paracords__c), pd.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();
    String strMessage = '';
    //Iterating through the sorted Integer list in reverse order (descending) to get the final String message from    //the map 
    for(Integer I = 0,  I < lstNumValue.size(); i++)
    {
        strMessage += mapNumValueAndFieldLabel.get(lstNumValue(i)) + ', ';
    }
 }
  //create new long text area field to store your string values
    pd.Your_New_Long_Text_Area_field__c = strMessage;
   }

}
Medhanie HabteMedhanie Habte
Almost there @KetanKumar

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.

 
for(Integer I = 0,  I < lstNumValue.size(); i++)