Dynamics CRM 2011 · Javascript

CRM 2011: Filtered Lookup using Javascript

I came across a requirements where a lookup field available values need to be filtered based on certain value or user settings.

To do this, we need to replace the view (query and layout) of the lookup control.

1. Query
The best way to do this is to use Advanced Find to construct the query and then click on ‘Download Fetch Xml’ button. The query xml should look something like this:

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
  <entity name="account">
    <attribute name="name" />
    <attribute name="primarycontactid" />
    <attribute name="telephone1" />
    <attribute name="accountid" />
    <order attribute="name" descending="false" />
    <filter type="and">
      <condition attribute="name" operator="like" value="Abc%" />
    </filter>
  </entity>
</fetch>

2. Layout
This is the view that will be displayed. Modify this to suit your needs, it should match the query result above. The layout xml should look something like this:

<grid name="resultset" object="1" jump="name" select="1" icon="1" preview="1">
  <row name="result" id="accountid">
    <cell name="name" width="300" />
    <cell name="primarycontactid" width="150" />
    <cell name="telephone1" width="100" />
  </row>
</grid>

3. Replace the lookup control view

Xrm.Page.getControl("<lookup field name>").addCustomView("<viewId>", "<entityName>", "<viewDisplayName>", "<fetchXml>", "<layoutXml>", true);

The complete script should look something like this:

var viewId = "{1DFB2B35-B07C-44D1-868D-258DEEAB88E2}"; //Random GUID
var entityName = "account";
var viewDisplayName = "Accounts beginning with Abc";

var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
				  "<entity name='account'>" +
					"<attribute name='name' />" +
					"<attribute name='primarycontactid' />" +
					"<attribute name='telephone1' />" +
					"<attribute name='accountid' />" +
					"<order attribute='name' descending='false' />" +
					"<filter type='and'>" +
					  "<condition attribute='name' operator='like' value='Abc%' />" +
					"</filter>" +
				  "</entity>" +
				"</fetch>";

var layoutXml = "<grid name='resultset' object='1' jump='name' select='1' icon='1' preview='1'>" +
				  "<row name='result' id='accountid'>" +
					"<cell name='name' width='300' />" +
					"<cell name='primarycontactid' width='150' />" +
					"<cell name='telephone1' width='100' />" +
				  "</row>" +
				"</grid>";

Xrm.Page.getControl("new_accountid").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);

PS:
You can also use getDefaultView() and setDefaultView() to work around the default lookup view.

var defaultViewId = Xrm.Page.getControl("<lookup field name>").getDefaultView();

if (needCustomView) {
	//Custom view magic
}
else if (needDefaultView) {
	Xrm.Page.getControl("<lookup field name>").setDefaultView(defaultViewId);
}

Cheers – Sy

Advertisements

5 thoughts on “CRM 2011: Filtered Lookup using Javascript

  1. Hi, thanks for useful post. I have a one question:
    Does the complete script should be added to the lookup’s onChange event ?
    Thanks!

    1. In short yes. Although, the important thing is that the script is available to the lookup field’s onChange event (referenced in the form).

      Sy

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s