Sorting picklist values: External objects with skuid

  • 2
  • Question
  • Updated 3 years ago
  • Answered
I have a lookup reference field on "Event" external object that returns the looked up values from another external object "Region Code" in a picklist (which makes sense, as there are only 20 or so records in the looked up object). But I want the picklist entries to be in a sort order based on another field on external object "Region Code" called 'RegionID__c' (which is the combination of text and number like: region 1, region 2, ...region 11, region 12: (Salesforce 'Text' field)). I am using the field's "Advanced" properties to order them by "RegionID__c". 

I am able to display like: region 1, region 10, region 11....region 2, region 21...
I would like to display like: region 1, region 2, region 3.....region 10, region 11.

Any help is appreciate. I would like to know is there any setting that is available with skuid out of the box or else do i need to use the custom renderer (writing code)?
Photo of Goutham J

Goutham J

  • 568 Points 500 badge 2x thumb

Posted 3 years ago

  • 2
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,004 Points 20k badge 2x thumb
To Order/Sort the results that show up in the Reference field Picklist renderer, you should do 2 things:

1. Add a Model to the page to load in your Regions. With the Model's properties, you can set the Order By clause directly, e.g. ordering them by "RegionID__c". 
2. Set your field's "Option Source" to Model, and select your Regions model as the Option Model.

This community post describes this approach in more detail.  In this post the goal is to be able to show more than 200 picklist values, which is another reason / use case for using Model as your field's Option Source.
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Zach,

Won't Goutham still have the same problem?

Ordering by a text field will always be alphabetical, and return
region 1, region 10, region 11....region 2, region 21...

rather than
region 1, region 2, region 3.....region 10, region 11

Right? I could actually be totally wrong here... but I guess it seems to me that using the advanced properties of the field to set the Order By clause and using a new model to set the order by clause would have the same result.


Assuming that's the case...

Goutham, if you can create a formula field on your object that returns a number (simply strip out 'region ' from your RegionID__c), you could order by that new field.
(Edited)
Photo of Goutham J

Goutham J

  • 568 Points 500 badge 2x thumb
I appreciate you reply.

Zach McElrath- What ever you have mentioned above is something that I am already having now.

Matt Sones - Yeah still I am having the same problem now, Here I am dealing with Lightning connect external objects, as of now external objects doesn't support any formula fields.


Please let me now if there are any other possibilities.
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,004 Points 20k badge 2x thumb
If there aren't any fields on your External Object to order by that will work, since you can't create Formula Fields, you probably need to use a Custom Field Renderer to reorder the values in the picklist.
Photo of Goutham J

Goutham J

  • 568 Points 500 badge 2x thumb
Sure will do that Zach.

Thanks,
Goutham
Photo of Goutham J

Goutham J

  • 568 Points 500 badge 2x thumb
Zach,

Can you please share any sample snippet (Custom Field Renderer) to do this? I am getting started with skuid.


Thanks,
Goutham
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Goutham,

This thread should provide you with more than enough fodder to create your snippet:

https://community.skuidify.com/skuid/topics/how_do_i_render_custom_picklist_values

In particular, this code from Moshe is stripped down to the basics:

var field = arguments[0],
    value = skuid.utils.decodeHTML(arguments[1]);
    var picklistEntries = field.metadata.picklistEntries;
    picklistEntries.length = 0;     // if you don't do this, then the "real" values are already in the picklist and the code below will add duplicate values
	picklistEntries.push(
        { value: 'Fixed', label: 'Fixed', defaultValue: false, active: true },
		{ value: 'Indexed', label: 'Indexed', defaultValue: false, active: true }
	);
// Run the standard picklist renderer for the given mode
skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field,value);
(Edited)
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Oops, the first line got left our of the <pre> tag...

var field = arguments[0],
value = skuid.utils.decodeHTML(arguments[1]); var picklistEntries = field.metadata.picklistEntries; picklistEntries.length = 0; // if you don't do this, then the "real" values are already in the picklist and the code below will add duplicate values picklistEntries.push( { value: 'Fixed', label: 'Fixed', defaultValue: false, active: true }, { value: 'Indexed', label: 'Indexed', defaultValue: false, active: true } ); // Run the standard picklist renderer for the given mode skuid.ui.fieldRenderers[field.metadata.displaytype][field.mode](field,value);
Photo of Goutham J

Goutham J

  • 568 Points 500 badge 2x thumb
Matt,

I appreciate you reply, above mentioned code will work for standard pick-lists.
In my case, I am dealing with custom pick-list. Let me put it this way.

we have two external objects "Events" and "Region", having an external look-up between "Events" and ""Region". At the time of event creation we are displaying the "Regions" as pick-list for which I have created a model in the events skuid page called "Regions model" and by using the custom snippet (Field rendering) for those regions I am able to display them in the pick-list. Here is the code I have for that.

var regions = [];    regions.push({
         active: true
        ,defaultValue: true
        ,label : '--None--'
        ,value : ''
    });
    
    skuid.$.each(skuid.model.getModel('Regions').getRows(), function(i,row) {
        regions.push({
            active: true
            ,defaultValue: false
            ,label : row.RegionCode__c
            ,value : row.RegionID__c
        });
    });
    
    var customSelect = skuid.ui.renderers.PICKLIST.edit({
        entries : regions,
        required : true,
        value : value
    }).change(function(newValue) {
        var selectedOptions = '';
        for(i = 0; i < newValue.target.selectedOptions.length; i++) {
            //There will only be one selected option since it's rendered as a picklist
            //But I wrote this originally to do something similar with multiselects
            selectedOptions += newValue.target.selectedOptions[i].value;
        }
        console.log('selectedOptions=' + selectedOptions);
        //Update the row in the target object
        field.row[field.id] = selectedOptions;
        //field.model.updateRow(field.row, field, selectedOptions);
    })
    //Append the MULTIPICKLIST to the DOM element
    field.element.append(customSelect).addClass('requiredField');
});


In the "Region" model I am ordering regions by "RegionCode__c". But no help, since "RegionCode__c" is the combination of 'Text' and 'numeric'. Could you please suggest me anything, that can help me.
Photo of Matt Sones

Matt Sones, Champion

  • 31,478 Points 20k badge 2x thumb
Goutham,

The only thing I can thing to do is to create an object or array with the values from your Regions model, sort that array, and then use the sorted array to push your picklist values.

Here's a stab at it... not sure if this will work?

var regionsSortArray= [];
skuid.$.each(skuid.model.getModel('Regions').getRows(), function(i,row) {
        regionsSortArray.push(row.RegionID__c);
});
var regionsSortObject = {};
skuid.$.each(regionsSortArray, function(i, regionId) {
       //Put just the number from the string "region n" into the keys, and the whole string into the value
       regionsSortObject[regionId.slice(7)] = regionId;
});

From there you should be able to sort regionsSortObject by its keys, and then push its values into your picklist. Does that make sense?
(Edited)
Photo of Goutham J

Goutham J

  • 568 Points 500 badge 2x thumb
Thanks Matt, Yeah It makes sense. I will give it a shot:-).


Thanks,
Goutham