Search by default with lookupObjects JavaScript in Dynamics 365

Paul Nieuwelaar, 22 September 2020

Microsoft have made some great improvements to the lookupObjects function for the Unified Interface, including the support for the searchText parameter. This used to work in the old interface, but was never officially documented, and wasn’t working in the Unified Interface up until the 2020 Wave 1 release. The searchText parameter as well as a few other useful properties (like disableMru) are now documented and supported in the Unified Interface.

I won’t go into a lot of detail about what the searchText parameter does, as it’s quite self-explanatory. The one thing I want to touch on is a unique feature that searchText provides, which is that when a value is provided, the lookupObjects window performs the search automatically by default. I’ll show how we can exploit this to trick the system into thinking we’ve done a search when we haven’t, meaning the user doesn’t have to press enter to see the available records.

Normally when you open a lookupObjects window, e.g. using the following simple code which just shows all accounts (note that I’ve disabled most recently used), the window that opens shows no records by default, and instead we have to press enter to actually search.

var lookupOptions = {
    entityTypes: [ "account" ],
    disableMru: true
};

Xrm.Utility.lookupObjects(lookupOptions);

image

This can be quite annoying for users, especially if you’re already filtering the results, or if you have a small set of data to select from. Once you press Enter or type something to search, it shows all the results.

When we provide a searchText parameter, the system automatically performs the search by default, meaning we don’t have to press Enter to see those initial results.

var lookupOptions = {
    entityTypes: [ "account" ],
    disableMru: true,
    searchText: "A"

};

Xrm.Utility.lookupObjects(lookupOptions);

image

As you can see, we now get those results instantly without having to press Enter first, but we’re forced to provide a search term. If we provide an empty string "", the system still doesn’t perform the search. If we provide a blank space " ", the system does show us all the results, however the search box also puts that space in, which can be confusing for users if they then try to search (which won’t work with a leading space).

So, what can we do? If we pass in a line break "\n" as the searchText, the system will both perform the search by default, and leave the search box blank if the user needs to actually search.

var lookupOptions = {
    entityTypes: [ "account" ],
    disableMru: true,
    searchText: "\n"

};

Xrm.Utility.lookupObjects(lookupOptions);

image

If you’re using the searchText already, you can do something like:

searchText: someValue || "\n"

This will make sure you’re always telling the system to search by default, so the experience is consistent for users whether a default searchText is provided or not.

This is just a simple little workaround to save users a step when picking records from the lookupObjects window. Hopefully we get some proper support for something like this in the future, as this isn’t guaranteed to work forever, however it’s fairly low risk if it does stop working.