Custom Metadata Type not being sorted as expected
Apex , API , Custom Objects , Flow
Last updated 2022-05-31 ·Reference W-3568979 ·Reported By 179 users
In Review
Summary
Sorting order not being used in SOQL with Custom Metadata Type when sort field is not in column queried. This result in the order not being returned properly with Flows, API, Apex, and any functionality which would use a SOQL query for Custom Metadata Type.
Consider a Custom metadata type called MD_NUMBER__mdt with a custom field value__c of Type NUMBER.
Execute the following SOQL query from workbench
SELECT Id,Label FROM MD_NUMBER__mdt ORDER BY value__c ASC
Actual Result:
Id Label
1 m00xx000000000IAAQ FOUR
2 m00xx000000000HAAQ ONE
3 m00xx000000000NAAQ THREE
4 m00xx000000000MAAQ TWO
Expected Result:
Id Label
1 m00xx000000000HAAQ ONE
2 m00xx000000000MAAQ TWO
3 m00xx000000000NAAQ THREE
4 m00xx000000000IAAQ FOUR
Repro
1. Creating custom metadata type and records:
1.Goto Setup-->Develop-->Custom Metadata Types
2. Click NewCustomMetadataTpe
>Label:MD_NUMBER
>Plural Label:MD_NUMBERS
>Click save
3. Under Custom fields, click new
4. Field Type: Number; click next
5. Field Label: value; click next and save
6. Click Manage MD_NUMBER and click new to start adding records for this type
>Label: ONE
>Value: 1
7. Repeat the above step to add TWO(2), THREE(3) and FOUR(4)
2. Launch workbench for the org and execute the following SOQL and observe the results
SELECT Id,Label,value__c FROM MD_NUMBER__mdt ORDER BY value__c ASC
Id Label value__c
1 m00xx000000000HAAQ ONE 1
2 m00xx000000000MAAQ TWO 2
3 m00xx000000000NAAQ THREE 3
4 m00xx000000000IAAQ FOUR 4
SELECT Id,Label FROM MD_NUMBER__mdt ORDER BY value__c ASC
Id Label
1 m00xx000000000IAAQ FOUR
2 m00xx000000000HAAQ ONE
3 m00xx000000000NAAQ THREE
4 m00xx000000000MAAQ TWO
Workaround
You can only use ORDER BY when the ordered field is a selected field.
Reported By (179)































Is it Fixed?
Any unreleased services, features, statuses, or dates referenced in this or other public statements are not currently available and may not be delivered on time or at all. Customers who purchase our services should make their purchase decisions based upon features that are currently available.