Lookup Column | orderBy | not working

Hello,

Prior to the most recent update, I was able to sort the items in a Lookup field's drop-down using the following code:
fd.spRendered(function () {
fd.field('_MyLookup').ready().then(function () {
// sort lookup values
fd.field('_MyLookup').orderBy = { field: 'Title', desc: false };
});
});

However, this is no longer working.

Am I doing this in the wrong events?

Hello @ShareSquared,

Please try to add a refresh() to the code like this:


fd.spRendered(function () {
fd.field('_MyLookup').ready().then(function () {
// sort lookup values
fd.field('_MyLookup').orderBy = { field: 'Title', desc: false };
fd.field('_MyLookup').refresh();
});
});
1 Like

That did it, thank you!

1 Like

@mnikitina Please please please can you make the default sorting by the lookup column rather than the ID? Or at least add it to the options of the lookup in the GUI as if you have a form with many lookups, add this is quite long :slight_smile:

Hello @Jamal_Smith-Graham,

That is a great idea!

Are you considering a paid support option?

We can add an "order by" option for the lookup field to the UI, so you can choose the sort field and sort type directly in the designer. If you are interested, please contact us by email: support@plumsail.com.

Dear Nikitina

I am trying to solve the same problem and it's not working for me.

This is the code i used. "Document to Change_x" is a Lookup Field to a Calculated Field in another List. and i'm trying to sort by Document-ID which is a Number-Field in referenced List.

Is this even possible to sort by another field that is in the same list as the LookUp column-Field?

And if possible i would like to have this function build in already in the GUI for Forms.
Maybe if its possible some sort of function like depends on where i can select another field in the referenced lookup list and then sort descending or ascending.

I think you have to include any extra fields in the Extra Fields and/or the Expand property before you can use them.

Whether or not you can use them for sorting, or how to do it, is something I will leave for the more smarted folks :wink:

@Dario_Chiga,

The code is correct, the problem is in the field name. Document-ID is an invalid field name. You can find the internal field name in the designer:
image

Or in the SharePoint List Settings >> Field Settings >> Page URL:
image

We are planning to add this property to the designer in the future, but there is no release date yet. If you need this functionality straight away, we can switch task priority, but that would be paid support. If you are interested, please contact us by email: support@plumsail.com

Can the Internal Name of the Column to be sorted by, be any of the Fields in the referenced List or does it have to be the same column, that is referenced in the LookUp Column?

@Dario_Chiga,

You can order Lookup values by any field in a source list, except calculated columns and fields that allow multiple selections.

You can find more information about how Odata query is built in Microsoft documentation here:

If the ordering still doesn't work, please check the browser console (F12) for errors and share it screenshot.

It still doesnt work.

Now this is my full code i use on the "New"-Form of this List.

The Field "Document_x0020_to_x0020_Change_x" is a LookUp Column to a calculated field.
And i'm trying to sort by "Document-ID" which is in the source list.
the Internal-Name of this field is "Document_x002d_ID" which i also implemented like this in the code as you can see.

One of the Errors i get is this one:

Now i think i may be doing everything correctly as you explained to me but it somehow still doesnt work.

Dear @Dario_Chiga,
First of all, regarding the error - either one of the fields that you're trying to show/hide doesn't exist, has different Internal Name, or it's simply not ready() when you try to call it.

For example, before running any code on a lookup field, it's highly recommended to wait for it to finish rendering with the ready() method instead of running it on load, like this, for fields Lookup and Lookup2:

fd.field('Lookup').ready().then(function() {
    fd.field('Lookup2').ready().then(function() {
      hideOrShow();
    });
});

Regarding ordering - Calculated fields cannot be ordered directly, just like they cannot be filtered. Instead of trying to order by calculated field, you need to replicate calculation in your code.

For example, if you calculated field has "Title + ID", you need to order by Title and ID fields instead.

I modified my code to look like this:

Somehow it didn't work when HideOrShow() Function was called before the ready() event but when it's launched after the ready() event it works perfectly.

Thank you very much for your support and patience.

Dear @Dario,
First of all, sorting would never work for a Calculated field, you need to sort by something else.

Secondly, you're currently running SortFields 3 times, and that's not what needed. ready() should be nested, like in my example.

If you want us to help with the code - you need to paste the code here, and not just a screenshot as we can't edit it.

1 Like

Dear @nikita

Thank you for the Tipp with the nested ready() function.
I will change it like you said, but it works also in my example.
but i guess your take on this problem is way cleaner for this kind of scripting.

Dear @Dario_Chiga,
Not nested should theoretically work, as you run it 3 times, and during the last run all fields will be ready, but it's impossible to say which one will be ready first, so instead of running it 3 times, best to just run once when you're sure all fields are ready.

Same applies to hideOrShow(); function

Alright. Thank you very much for this Information
I applied it like you said and it works perfectly.

Thank you