Create a custom filtered lookup view in runtime – CRM 2011

Starting CRM 2011 you can filter a certain lookup view based on related entities or other system/custom views. But sometimes this is not enough. For example, if you wanna make a custom filtered view, that uses several filters, you can use the following approach.

Starting scenario (all of the completed lookup fields will be used as filters):


// *** ALL IN 1 CODE ***
// Structs
var FilterBy = MakeStruct("SchemaName Operator Value");
var ViewColumn = MakeStruct("SchemaName Width");

// Advanced Filtered Lookup
function AdvancedFilteredLookup(lookupSchemaName, viewId, entityName, primaryKeyName, primaryFieldName, viewDisplayName, filterBy, orderBy, viewColumns) 
{
	var fetchXml = "" +
	"" +
	"" +
	"" +
	"" + 
	"";	
	for(var i=0; i< filterBy.length; i++)
		fetchXml += "";	
	fetchXml += "";
	
	var layoutXml = "" +
	"";

	for(var i=0; i< viewColumns.length; i++)
		layoutXml += "";
	layoutXml += "";

	try {		
		var lookupControl = Xrm.Page.ui.controls.get(lookupSchemaName);
		lookupControl.addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
	}
	catch(err) {
	}
}

// USE - Filter Products Runtime LookUp View
function FilterProducts() 
{
	try {		
		// Parameters
		var customViewId = "{FD140AAF-4DF4-11DD-BD17-0019B9312238}"; // new id
		var customViewName = "Type Filtered Products";
		var lookupFieldName = "new_existingproductid";
		var entityName = "product";
		var primaryKeyName = "productid";
		var primaryFieldName = "name";
		var orderBy = "name";
		
		// Generate Filters
		var type1id = crmForm.all.item("new_type1id").DataValue != null ? crmForm.all.item("new_type1id").DataValue[0].id : null;
		var type2id = crmForm.all.item("new_type2id").DataValue != null ? crmForm.all.item("new_type2id").DataValue[0].id : null;
		var type3id = crmForm.all.item("new_type3id").DataValue != null ? crmForm.all.item("new_type3id").DataValue[0].id : null;
		var type4id = crmForm.all.item("new_type4id").DataValue != null ? crmForm.all.item("new_type4id").DataValue[0].id : null;
		var clientneedid = crmForm.all.item("new_clientneedid").DataValue != null ? crmForm.all.item("new_clientneedid").DataValue[0].id : null;		
		var vendorid = crmForm.all.item("new_vendorid").DataValue != null ? crmForm.all.item("new_vendorid").DataValue[0].id : null;	
		var filters = new Array();
		var index = 0;		
		if(type1id != null) 
			filters[index++] = new FilterBy("new_type1id", LogicalOperator.Eq, type1id);		
		if(type2id != null) 
			filters[index++] = new FilterBy("new_type2id", LogicalOperator.Eq, type2id);		
		if(type3id != null) 
			filters[index++] = new FilterBy("new_type3id", LogicalOperator.Eq, type3id);		
		if(type4id != null) 
			filters[index++] = new FilterBy("new_type4id", LogicalOperator.Eq, type4id);		
		if(clientneedid != null) 
			filters[index++] = new FilterBy("new_clientneedid", LogicalOperator.Eq, clientneedid);
		if(vendorid != null) 
			filters[index++] = new FilterBy("nov_vendorid", LogicalOperator.Eq, vendorid);
		
		// View Columns
		var viewColumns = [new ViewColumn("name", 200), new ViewColumn("productnumber", 100), new ViewColumn("new_type1id", 100), new ViewColumn("new_type2id", 100), new ViewColumn("new_type3id", 100), new ViewColumn("new_type4id", 100), new ViewColumn("new_clientneedid", 100), new ViewColumn("nov_vendorid", 100)];

		// Create Dynamics View
		AdvancedFilteredLookup(lookupFieldName, customViewId, entityName, primaryKeyName, primaryFieldName,  customViewName, filters, orderBy, viewColumns);

		// Clear Previous Product Value
		crmForm.all.item(lookupFieldName).DataValue = null;
	}
	catch(err) {
	}
}

And now, the custom filtered lookup view for the new_productid lookup field will look like this:


Hope you’ll find this article useful.

P.S.: In my opinion, filtering a lookup this way is a lot more efficient than altering the lookupsingle.aspx file (the unsupported method used in CRM 4.0) 😉

10 comments

  1. This is great; thanks for sharing! Is there an additional library needed for it to work? I get an error that the LogicalOperator is not defined.

  2. Did you get the order piece of it to work?

    I didn’t try your solution, just was looking for others having the same problem. Looking at your screen shot it would appear the order attribute wasn’t respected (no order icon on the columns). I’m pretty sure its a bug in CRM 2011, but thought I’d ask.

  3. Multumesc pentru info, foarte folositoare. Totusi am o intrebare, nu am gasit nici o solutie la urmatoarea problema, poate stii tu raspunsul. Am incercat sa modific “a query” dinamic, astfel incat “a lookup field” sa-mi arate doar “entities” la care suma a doua “of its attributes” mai mare decat X. Ma gandesc ca nu-i posibil, dar totusi m-ar ajuta sa am o confirmare, astfel nu-mi mai bat capul.

    Multumesc.

  4. Hi Cornel,

    thank you for publishing this solution. I miss a part, where I have to integrate the JavaScript into the Lookup form? Is this the standard CRM lookup form? How can I customize it? Is the FilterProducts the “typical” crm method called on clicking on the search button in the lookup form? Thank you in advance for the missed information,
    Gabriella

  5. Hello Gabriella,

    No, FilterProducts it’s not a standard method, but custom. You can include it in a custom .js resource in your solution on use it on the OnLoad() event of the form. It simply uses the AdvancedFilteredLookup(…) method to alter the fetchXML of the lookup window in runtime, so you can filter the products grid using your custom criteria. Hope this helped.

    If you got further questions, please ask.

    Thank you,
    Cornel

Leave a comment

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