Question

I've created a filtered view for a lookup via javascript that gets it's filter value from a picklist. This works fine except I also want to remove the ability for the user to select one of the System Views.

If I set the View Selector option to Off on the lookup in Form Designer this correctly disables view selection but my filtered view is also included in this exclusion and therefore my filter isn't applied.

How can I use both my custom filtered view whilst also preventing the user from selecting any other system view?

My only idea so far would be to provide a default view with a filter that always returns no records and set this as the only allowable view selection. This will ensure the user only ever gets to select records in my filtered view.

Was it helpful?

Solution

I've arrived at an acceptable solution for this that I'll summarise here for others and will follow up with a blog post that I'll link.

  1. Create a new View for the entity you want to filter. Name is something contextual like Customers Filtered By Favourite Drink. We'll call this view FV.
  2. Add the lookup attribute to your target form and set the default view to FV
  3. Set View Selector to Off (which was previously causing me the problems but will be "fixed" in the javascript)
  4. Use one of the many samples of code for creating a filtered lookup in javascript including the one in the SDK. However, rather than giving this view a new GUID give it the GUID of the default view (which in our case is FV). You can do this by calling lookup.getDefaultView() where lookup in the target lookup attribute.

You will now have a lookup that has view selection disabled but it will now show your filtered results.

OTHER TIPS

In addition to Darren's answer.

You can do the same steps without adding a new view.

Just get the guid of the default view:

var availableDefaultViewId = Xrm.Page.getControl(lookupField).getDefaultView();

and then set it to your new FV :

Xrm.Page.getControl(lookupField).addCustomView(availableDefaultViewId , entityName, viewDisplayName, fetchXml, layoutXml, true);

But, your viewDisplayName won't work, it will get the default's view name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top