Please read simple filter lookup in ms crm2011, my last post before implement this.
Let’s take a simple scenario, we have custom unit lookup in opportunity product entity. We want to filter Custom Unit Lookup based on different product’s set to Custom Media entity where Media and Product entity have many-to-many relationship.
1. Use link-entity in FetchXML
Here is the code sample:-
function SetUnitFilterLookup() {
if (Xrm.Page.getAttribute("new_mediaid").getValue() != null) {
var _Value = Xrm.Page.getAttribute("new_mediaid").getValue()[0].id;
var viewId = "{65EC9B45-EE81-4F89-BAF6-E8603FF8E1E4}";
var entityName = "uom";
var viewDisplayName = "Active Units For Media"
var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
"<entity name='uom'>" +
"<attribute name='uomid'/>" +
"<attribute name='name'/>" +
"<link-entity name='product' to='uomid' from='defaultuomid' visible='false' intersect='true'>" +
"<link-entity name=new_new_media_product' to='productid' from='productid' visible='false' intersect='true'>" +
"<link-entity name=new_media' alias='ab' to=new_mediaid' from=new_mediaid' visible='false' intersect='true'>" +
"<filter type='and'>" +
"<condition attribute=new_mediaid' value='" + _Value + "' operator='eq'/>" +
"</filter></link-entity></link-entity></link-entity></entity></fetch>";
//build grid layout
var layoutXml = "<grid name='resultset' " +
"object='1' " +
"jump='name' " +
"select='1' " +
"icon='1' " +
"preview='1'>" +
"<row name='result' " +
"id='uomid'>" +
"<cell name='name' " +
"width='300' />" +
"</row>" +
"</grid>";
//add new view view
Xrm.Page.getControl("new_unitofsaleid").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
}
}
Call this function on OnChange Event of Media Lookup field.
Hope it helps someone!!!