1

What is the order rule of multiple picklist values?

When I try to SELECT global multiple picklist values by SOQL which is used in custom object, the order of multiple values is not same as the order of global multiple picklist values, and does not look same as alphabetic order.

2
  • 1
    Have you tried includes? developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/… Jul 22, 2019 at 9:51
  • Are you talking about the order in which selected values for a multi-select picklist appear in the field on a record? The order is not determinable. If a user selects 2 values and saves, and then edits and selects a 3rd value and saves, the new value is appended to the end of the semicolon-delimited list, regardless of the order of the values in the UI or in global picklist value setup. Jul 22, 2019 at 12:09

1 Answer 1

1

My interpretation and response to this question is that the important thing here is SOQL behaviour with the INCLUDES term, rather than exactly how the multi picklist value is returned in apex.

For example, if I have a global value set with two picklist values "Pay" and "Charge" and then have a multi picklist field called Payroll__c in my custom object type MyObject__c, I can do something like:

List<MyObject__c> myObjects = new List<MyObject__c> {
        new MyObject__c(Payroll__c = 'Pay;Charge'),
        new MyObject__c(Payroll__c = 'Charge;Pay'),
        new MyObject__c(Payroll__c = 'Pay')
};

insert myObjects;

List<MyObject__c> fetched = [
    SELECT Id, Payroll__c FROM MyObject__c
        WHERE Payroll__c INCLUDES ('Charge;Pay')
];

System.debug(JSON.serialize(fetched));

delete myObjects;

See how this queries MyObject__c records that have both "Pay" and "Charge" selected using "Charge;Pay" (so out of order for at least one of the input records). The resulting debug details (when stripped down and prettified) may look like:

[
  {
    "Id": "a2D0U000000uQoOUAU",
    "Payroll__c": "Pay;Charge"
  },
  {
    "Id": "a2D0U000000uQoPUAU",
    "Payroll__c": "Pay;Charge"
  }
]

See that it didn't matter the order of the picklist values during the insert, nor the order of the values in the INCLUDES.

In my tests I found it returned the values in the order I added the values to the global value set, but since there's no specified documentation I don't think it is wise or valid to assume this is the case and I need to always check this by using logic like:

Set<String> need = new Set<String> { 'Pay', 'Charge' };

for (MyObject__c record : fetched) {
    System.debug('Match for ' + record.Id + ' is ' + need.equals(new Set<String>(record.TimesheetTypes__c.split(';'))));
}

(Obviously in this case since the INCLUDES of the SOQL and the need set both contain the same values, I get true for each of the fetched (i.e. requeried) records. Applying this logic to the original myObjects gives true, true and false, as we would expect.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .