Blog, Development, Troubleshooting

Xrm.Utility.lookupObjects: Episode 3: The UCI strikes back

Today was the last day of the North America UGSummit. It was my 3rd UGSummit and I’m already prepared to pack my bags to travel to next year’s UGSummit in Nashville, TN. I like it a lot for many reasons – reconnecting with my MVP Family, thought-provoking sessions, networking, and the “CRMUG Medics” booth. For those who don’t know what the “CRMUG Medics” booth is, just imagine a place where you can go and ask questions you have about development, administration or troubleshooting e.t.c. and that booth almost always has a couple of MVPs that are ready to answer your questions. I really enjoy helping others by answering their questions, so I was hanging out trying to help visitors and I got asked a question:
“In our code we used Xrm.Utility.lookupObjects with additional filters. When we are running the scenario in the Classic Interface everything works fine, but it doesn’t work in UCI. What can I do to make the code work in both interfaces?”

I recalled that I wrote a post about Xrm.Utility.lookupObjects function almost 2 years ago when 9.0 was just shipped. I opened the post quickly and compared their code with what I had written in my post. It looked really similar to what I had. Let’s say it was following:
var lookupOptions = {
	defaultEntityType: "account",
	entityTypes: ["account"],
        allowMultiSelect: false,
        customFilters: ["%3Cfilter%20type%3D%22and%22%3E%3Ccondition%20attribute%3D%22name%22%20operator%3D%22eq%22%20value%3D%22test%22%20%2F%3E%3C%2Ffilter%3E"],
	customFilterTypes: ["account"]
};

Xrm.Utility.lookupObjects(lookupOptions)
	.then(function(result){
	})
	.fail(function(error){
	});

After that, I recalled that I saw the post in the community forum that code works for Classic Interface but doesn’t work in UCI, but I was busy with my project tasks and had no time to troubleshoot so I put that idea aside and forgot about it. Until that same question appeared during the UGSummit.

To assure myself that the code was still valid, I went to the doc.microsoft.com page that describes this method and I discovered that a couple of the new properties were introduced but the most intriguing was the “filter” property. Here is the screenshot from the official documentation:

That was exactly what had to be used! So I suggested to them to rework the code in the following way:
var lookupOptions = {
	defaultEntityType: "account",
	entityTypes: ["account"],
        allowMultiSelect: false,
	filters: [{
		filterXml: "<filter type='and'><condition attribute='name' operator='eq' value='test' /></filter>",
		entityLogicalName: "account"
	}]
};

Xrm.Utility.lookupObjects(lookupOptions)
	.then(function(result){
	})
	.fail(function(error){
	});

The code was refactored, then published, and everything worked fine in UCI, but at the same time stopped working in the Classic Interface.

To make the code function properly again in both interfaces, I suggested to combine both pieces of the code and, depending on the type of the UI used, pass the configuration of the filter that worked for the particular interface. This code uses an undocumented function for detection of the interface type, so please take it in consideration if you want to use that approach or not:

var lookupOptions = {
	defaultEntityType: "account",
	entityTypes: ["account"],
        allowMultiSelect: false
};

if (Xrm.Internal.isUci()) {
	lookupOptions.filters = [{
		filterXml: "<filter type='and'><condition attribute='name' operator='eq' value='test' /></filter>",
		entityLogicalName: "account"
	}];
} else {
        lookupOptions.customFilters = ["%3Cfilter%20type%3D%22and%22%3E%3Ccondition%20attribute%3D%22name%22%20operator%3D%22eq%22%20value%3D%22test%22%20%2F%3E%3C%2Ffilter%3E"];
	lookupOptions.customFilterTypes = ["account"];
}

Xrm.Utility.lookupObjects(lookupOptions)
	.then(function(result){
	})
	.fail(function(error){
	});

 

17 Comments

      1. For some reason, the property disableMru is not working, after it is set as ‘true’. Tried it in D365 v9.0.5.5 and v9.0.17.

  1. Hello Andrew,

    Do you know, how to use linked-entity in filters? I am trying to pull only the team members in the same queue’s team and how how it is not working in UCI. Same thing works fine in older UI.

    filters: [{
    filterXml:
    “” +
    “” +
    “” +
    “” +
    “” +
    “” +
    “” +
    “”
    ,
    entityLogicalName: “systemuser”
    }],

  2. filters: [{
    filterXml:
    “” +
    “” +
    “” +
    “” +
    “” +
    “” +
    “” +
    “”
    ,
    entityLogicalName: “systemuser”
    }],

    1. Hello,
      There is no easy way to use linked entities directly. Also your fetchxml is turned to sequence of quotes. Can you please describe your scenario?
      Andrew

      1. Hi Andrew!

        I also need to build filter for custom lookup based on values in related entity, that is why I need to include link-entity element .

        1. Thanks for your reply
          But I’m not sure that it’s appropriate way for case where I want to use “lookupObject”
          I am going to override “Add existing record” button on form subgrid and open custom lookup

        2. Yaroslav,
          Got it. Slightly different scenario. In this I would recommend to check my previous post – https://butenko.pro/2017/11/22/microsoft-dynamics-365-v9-0-lookupobjects-closer-look/
          There is a way on how you can add a custom view to your lookup – but there is no guarantee that it’ll work because it’s undocumented feature so no guarantees that it’ll work fine in UCI.
          What you can do alternatively – get the list of records that will fit your requirement using Xrm.WebApi and fetchxml that utilizes link-entity and use that list in prefiltering that is supported in both interfaces.
          Andrew

  3. Hi Andrew, good afternoon.

    I have a custom html on iframe, that I need to open a lookup view to add price list items (code converts to quotedetail), to add on a quote.

    In classic interface works well, but on UCI don’t. I tried your solution to set correct view and it worked.

    But i’m having problems on search. It’s like if i type ‘12345’ and i know doesn’t exist, but list continues giving me all records.

    What can be?

    Thanks!

    1. Hi Felipe,

      I had the same problem with searching in a lookup dialog. For a lookup whose view was set to Price list Items in Entity Price List, adding PRODUCT ID column to the view worked for me. Now I can search products.

  4. Hi Andrew!

    Have you ever faced with an error “0x80072500 Invalid URI: The Uri string is too long.” in custom lookup window, while using Xrm.Utility.lookupObjects?

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.