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
Ryley HayesRyley Hayes 

Order of Selected Values in a Multi-Select Picklist

I'm attempting to find the "first" selected value in a multi-select picklist, and pass it into another field. I thought it would be fairly simple to do this - just take the string that the multi-select gets stored as, find the position of the first semicolon using indexof, then take everything before it using substring(0,firstsemicolon). And it works... sorta. The result is a single value from the selected values. But it's not the first value. To troubleshoot, I removed the indexof and substring and just wrote the value of the multi-select straight into the other field. So now I've got:

public class CaseFirstSymptom{
    public static void Symptoms (Case[] SymptomCases){
        for( Case SymptomCase : SymptomCases) {
            string FirstSymptom = SymptomCase.Symptoms__c;
            SymptomCase.First_Symptom__c = FirstSymptom;
        }
    }
}
On a record, I select the following values, and see this string displayed for that field after saving:
Complete (Pilot & Burner) Outage; Delayed Ignition; Does Not Light; Flare-Ups/Uneven Heat; Low Heat/Low Flame; Outage; Pressure Not Changing

The "target" field winds up displaying this:
Delayed Ignition;Complete (Pilot & Burner) Outage;Pressure Not Changing;Low Heat/Low Flame;Does Not Light;Outage;Flare-Ups/Uneven Heat

The values are all the same, but the order is changed.

I've got the picklist values in alphabetical order. I'm wondering if the values are ordered by ID or something though when you use that field in Apex. Anyone know for sure? And if so, anyone know a way around this?
Best Answer chosen by Ryley Hayes
Pramod_SFDCPramod_SFDC
Hi,

Can you try the below code, I hope it helps you.

List<Integer> intList = new List<Integer>();
Map<Integer, String> keyValMap = new Map<Integer, String>();
for(SelectOption i:listResult){
intList.add(Integer.valueOf(i.getValue()));
keyValMap.put(Integer.valueOf(i.getValue()),i.getLabel());
}
intList.sort();
listResult.clear();
for(Integer i:intList){
listResult.add(new SelectOption(String.valueOf(i),keyValMap.get(i)));
}



Regards
Pramod

All Answers

Pramod_SFDCPramod_SFDC
Hi,

Can you try the below code, I hope it helps you.

List<Integer> intList = new List<Integer>();
Map<Integer, String> keyValMap = new Map<Integer, String>();
for(SelectOption i:listResult){
intList.add(Integer.valueOf(i.getValue()));
keyValMap.put(Integer.valueOf(i.getValue()),i.getLabel());
}
intList.sort();
listResult.clear();
for(Integer i:intList){
listResult.add(new SelectOption(String.valueOf(i),keyValMap.get(i)));
}



Regards
Pramod
This was selected as the best answer
Gaurav NirwalGaurav Nirwal
you can try this code to resolve the problem 
List<Integer> intList = new List<Integer>();
Map<Integer, String> keyValMap = new Map<Integer, String>();
for(SelectOption i:listResult){
intList.add(Integer.valueOf(i.getValue()));
keyValMap.put(Integer.valueOf(i.getValue()),i.getLabel());
}