Filter lookups using JScript

This quick snippet comes in handy every now and then, and now it’s also tested for Dynamics CRM 2013.


I have a bunch of organizations, and a set of Organization Types. Let’s say, we have various client types, with various SLA’s.

In the Case entity, I want to show only the Organizations of specific type(s), that I can support through Cases.


First off, create a new web resource if one is not already created for the Case entity.

Then, from the form load event, call this function which uses addPreSearch.

function preFilterLookupOrganization() {   
    Xrm.Page.getControl("control_name").addPreSearch(function () {

This is from the SDK:


Use this method to apply changes to lookups based on values current just as the user is about to view results for the lookup.


Type: Function to add.
This method is only available for Updated Entities.

The argument is a function that will be run just before the search to provide results for a lookup occurs. You can use this handler to call one of the other lookup control functions and improve the results to be displayed in the lookup.


Next let’s build the handler. We’re building the filter query and add the filter to our control as such:

function addLookupFilter() {
    var myControl = Xrm.Page.getAttribute("control_name").getValue();

    if (myControl != null) {
        fetchXml = "<filter type=’and’><condition attribute=’customertypecode’ value=’1′ operator=’eq’/></filter>";

Now, it’s all in the filter, and the current example only looks for one specific Organization type. You can build that fetchXml filter as complex as needed, either by pulling if from an advanced find, creating it by hand, or using a 3rd party tool.


Leave a Reply

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

You are commenting using your 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 )

Connecting to %s

Create a website or blog at

Up ↑

%d bloggers like this: