Changing the default Sort of a Lookup Control in the Infor CRM Web Client

Normally, a lookup in the Infor CRM Web Client is determined based on the order of the columns as defined at design times. The search of the lookup defaults to the left most column in the layout and this is also the default sort of things that are returned.

There is a way to change a particular lookup’s default sort of the result set. Lets take a look at how.

The following code is the complete sample. Let’s see that and then I will explain it.

var lookupId = "lueProduct";
var defaultField = "ProdPackageKitType";

ScriptManager.RegisterStartupScript(this, GetType(), "script_CustomizeLookup", @"
    require(['dojo/aspect', 'Sage/UI/SDataLookup'], function(aspect, SDataLookup) {
        aspect.before(SDataLookup.prototype, 'initGrid', function() {
            if (this.id.indexOf('%LOOKUP%') !== -1) {
                this.storeOptions.sort = [{attribute:'%FIELD%'}];
            }
        });
    });
".Replace("%LOOKUP%", lookupId).Replace("%FIELD%", defaultField), true);

The lines

var lookupId = "lueProduct";
var defaultField = "ProdPackageKitType";

Define the name of the lookup control we want to modify (lueProduct) and what the property within the lookup layout we want to be the default sort field (ProdPackageKitType). The reason this code is written this way is to allow for it to be re-used with just the names needing to be replaced.

Most of the rest of the code is setting up a string that will be injected onto the page by the ScriptManager. This string is a dojo function utilizing aspect before to modify the base SDataLookup widget’s initGrid function. Within this function we are modifying the storeOptions.sort attribute to be the field we want to sort by. You could also add an asc or desc to change the direction.

ScriptManager.RegisterStartupScript(this, GetType(), "script_CustomizeLookup", @"
    require(['dojo/aspect', 'Sage/UI/SDataLookup'], function(aspect, SDataLookup) {
        aspect.before(SDataLookup.prototype, 'initGrid', function() {
            if (this.id.indexOf('%LOOKUP%') !== -1) {
                this.storeOptions.sort = [{attribute:'%FIELD%'}];
            }
        });
    });"

The final bit of code replaces the wildcards %LOOKUP% and %FIELD% with the names of the lookup and entity property defined at the start of the script.

....Replace("%LOOKUP%", lookupId).Replace("%FIELD%", defaultField), true);

All of this code would be added to the server-side Load Action of the quickform that the control resides on.

Note that the sort field needs to be a property in the lookup result layout.

ABOUT THE AUTHOR

Kris Halsrud

Kris Halsrud is a Senior Analyst / Developer for Customer FX Corporation.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter

Join our mailing list to receive the latest Infor CRM (Saleslogix) news and product updates!

You have Successfully Subscribed!