Client API – lookupObjects

clientapi-xrmmodel

New Client API

Lately, I’m exploring Client API that came with Dynamics 365 v9.0 and I can tell you that it has a bunch of cool stuff that is not used that frequently by the Dynamics community.

My top 3 namespaces are:

  • Xrm.Navigation (alert and confirm dialogs)
  • Xrm.Panel (side panel that makes some integrations look seamless)
  • Xrm.Utility (a bunch of new things)

In this post, I will show you one use case of a feature that made me so happy, because I always had a struggle when I tried to make look & feel of Dynamics lookup fields in my custom web resources.

This feature is of course Xrm.Utility.lookupObjects

The struggle

Every time I wanted to use lookup input field in my custom web resources I had the same problem. Which kind of control to use to present the lookup input field?

Sometimes I went the autocomplete route, but I had problems with validating the wrong values (user searched something and left the value that was not found in the textbox) or I went the dropdown route which was not that good option because it lacks a search option, but it forces user to choose the right value.

Solution

New API call Xrm.Utility.lookupObjects solved my problem and saved me some (BIG) amount of time. That new function allows you to open lookup browser with entities that you want to search for and returns the object containing record GUID, entity type of the record and a name of the record.

Example

My goal was to create a simple example that will display what you can do with the new function.

The goal is to create a popup window on a contact entity that will allow you to pick some system user in the popup and initiate a task entity creation that will have picked user as the owner of the task and the current contact in the regarding field.

I hope that example is simple enough to show you everything that is relevant while using this function.

Before we start making the solution I wanted to note that I really like Alert.js solution that makes popup windows in Dynamics so much easier. It has some downsides of course, but for the most challenges, it does a great job.

First, we need to make a simple HTML web resource that will be popped up. HTML code is shown in the code box below.

After we have created the HTML file we need to create a JS file that will invoke the actual logic.

There are 2 functions that are interesting here:

  • openLookupDialog
  • populateLookupValue

Function openLookupDialog is called when the user focuses the input element with name ID and it opens the lookup dialog.

There are few parameters that we need to pass to the openLookupDialog function.

Parameters:

  • entityTypes – list of strings containing entity names
  • defaultEntityType – string value of default entity
  • allowMultiSelect – bool value that controls multiple select

This is a minimum set of parameters that you need to pass to make this function work, but there are also other parameters that are mentioned in the official docs page.

The second function is populateLookupValue function that is called as success callback in the previous function. The parameter that is passed into this callback is a list of entities selected in the lookup dialog, but in our case, it returns only a single value because allowMultiSelect is set to false.

Each object inside that list has 3 values: id, name and typename which can be used in data operations after selection.

We use those values (or just a ID part) to create a task after valid record is selected.

Finally when we have the HTML and JS sorted we can add some CSS to get a Dynamics look and feel.

 

After everything is done right we can test our code and see the result. The result is shown in the next 3 steps.

STEP 1

Image 292

STEP 2

Image 293.png

STEP 3

Image 294

Conclusion

I hope that this example will help you when you will face the problem with using lookups in your custom web resources and as you can see it’s not that complex to use the new lookupObjects function in your solutions.

I like this function a lot because it gives the users similar UX as they are used to while using Dynamics.

2 thoughts on “Client API – lookupObjects

  1. Hey, very nice summary of this. I’d love to hear what those downsides of Alert.js are 😛 – always looking to improve the product, so if you have any suggestions or feedback feel free to let me know here or via Github. Also v3.0 of Alert.js makes use of this new LookupObjects function as well, which makes creating this sort of functionality even easier! Looking forward to your feedback. – Cheers, Paul

    • Hi Paul,

      One thing that annoyed me a lot regarding v2.1 is that you cannot make layered alerts, but I know (tried it and I can tell you that it is a great improvement) it’s fixed in v3.0. Another thing that I found is the issue with very fast transitions between alert windows that just refused to work (didn’t tested this one on the v3.0).

      Generally, I’m very happy that I can use Alert.js on almost every single project (every project since I found out the solution) that I worked on, but also when I tried to explain to the customer the benefit of getting the license for the v3.0 I get an answer “No, thank you.”. That’s maybe because we are working mostly in the Balkan region where you can’t expect that client will pay anything that is not a must-have tool for the project.

      Anyways I will continue to use v2.1 on every single project in future.

Comments are closed.