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
Jugbeer BholaJugbeer Bhola 

Picklist Stored Value Incorrect

A picklist exists that is custom ordered. Not alphabetic. 

The value displays on the User Interface as expected.  The 'Saved' picklist values are not physically in the order as displayed on the User Interface.  


This would not be important except that they are loaded into an XML file and are expected to be in the order they are displayed to the user.

Is there a way to retain the order of the picklist on the back side?  Or potentially sort based on the order of the picklist in Setup?  Not all values will be picked at all times and they need to be in the order represented in Setup. 

Example: Order of Picklist (Both represented in Setup and displayed to the User)

Asian
Black
Native
White
Other

A query exposes the fact that the values are acually stored like this.

Asian;Black;White;Native;Other
 

Best Answer chosen by Jugbeer Bhola
Jugbeer BholaJugbeer Bhola

Dislike this solution because it forces hard-coding.  Works.

 

static final String SEMI_COLON = ';';


public static String resortPicklist(String picklistValues, String pickAPIName) {
        String listFieldValue = '';
        if (pickAPIName == 'afb_Race__c') {
            acurateOrder.put('Native', 0);
            acurateOrder.put('Asian', 1);
            acurateOrder.put('Black', 2);
            acurateOrder.put('Hawaiian', 3);
            acurateOrder.put('White', 4);
            acurateOrder.put('Other', 5);
        }
        if (picklistValues == null) {
            return listFieldValue;
        }
        if (picklistValues.indexOf(SEMI_COLON) == -1) {
            return picklistValues;
        }
        String resortedPicklistValues = '';
        List<String> selectedPicklistValues = picklistValues.split(SEMI_COLON);
        List<ResortPicklist> pickListSorted = new List<ResortPicklist>();
        for (String pickListValue : selectedPicklistValues) {
            pickListSorted.add(new ResortPicklist(pickListValue));
        }
        pickListSorted.sort();
        List<ResortPicklist> pickListReSorted = new List<ResortPicklist>();
        for (ResortPicklist pickListValue : pickListSorted) {
            resortedPicklistValues = resortedPicklistValues + pickListValue.pickValue + SEMI_COLON;
        }
        resortedPicklistValues = resortedPicklistValues.removeEnd(SEMI_COLON);        
        return resortedPicklistValues;
    }

    public class ResortPicklist implements Comparable {
        private String pickValue { get; private set; }
        private Integer orderSequence;
        public ResortPicklist(String pickListValue) {
            this.pickValue = pickListValue;
            this.orderSequence = acurateOrder.get(this.pickValue);
            if (this.orderSequence == null)
                this.orderSequence = -1;
        }
        public Integer compareTo(Object compareTo) {
            ResortPicklist that = (ResortPicklist) compareTo;
            return this.orderSequence - that.orderSequence;
        }
    }

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Jugbeer,

Refer the below link have similar kind of issue.
https://salesforce.stackexchange.com/questions/10541/order-of-values-in-a-multi-select-picklist-is-not-consistent

If this helps, Please maek it as best answer.

Thanks!!
Jugbeer BholaJugbeer Bhola

Ankaiah, Thank you for making an effort to help solve this issue. 

Your link describes a situation concerning querining the database.  

Agreed, the creation of the picklist value is potentially involved.  

What Apex code may be executed to send the values stored, that matches the actual order of the picklist?   

The value are retrieved and pushed as stored.  They are not in the correct order base on the sort in Setup.

Since the postional value is not proper, the external system does not recieve the values as expected.

If you have an solution it would be appreciated.
Jugbeer BholaJugbeer Bhola

Dislike this solution because it forces hard-coding.  Works.

 

static final String SEMI_COLON = ';';


public static String resortPicklist(String picklistValues, String pickAPIName) {
        String listFieldValue = '';
        if (pickAPIName == 'afb_Race__c') {
            acurateOrder.put('Native', 0);
            acurateOrder.put('Asian', 1);
            acurateOrder.put('Black', 2);
            acurateOrder.put('Hawaiian', 3);
            acurateOrder.put('White', 4);
            acurateOrder.put('Other', 5);
        }
        if (picklistValues == null) {
            return listFieldValue;
        }
        if (picklistValues.indexOf(SEMI_COLON) == -1) {
            return picklistValues;
        }
        String resortedPicklistValues = '';
        List<String> selectedPicklistValues = picklistValues.split(SEMI_COLON);
        List<ResortPicklist> pickListSorted = new List<ResortPicklist>();
        for (String pickListValue : selectedPicklistValues) {
            pickListSorted.add(new ResortPicklist(pickListValue));
        }
        pickListSorted.sort();
        List<ResortPicklist> pickListReSorted = new List<ResortPicklist>();
        for (ResortPicklist pickListValue : pickListSorted) {
            resortedPicklistValues = resortedPicklistValues + pickListValue.pickValue + SEMI_COLON;
        }
        resortedPicklistValues = resortedPicklistValues.removeEnd(SEMI_COLON);        
        return resortedPicklistValues;
    }

    public class ResortPicklist implements Comparable {
        private String pickValue { get; private set; }
        private Integer orderSequence;
        public ResortPicklist(String pickListValue) {
            this.pickValue = pickListValue;
            this.orderSequence = acurateOrder.get(this.pickValue);
            if (this.orderSequence == null)
                this.orderSequence = -1;
        }
        public Integer compareTo(Object compareTo) {
            ResortPicklist that = (ResortPicklist) compareTo;
            return this.orderSequence - that.orderSequence;
        }
    }

This was selected as the best answer