Adding Additional Condition to Lookup View in CRM 2011 Based on the Current Default View (addCustomFilter for CRM 2011)
As we all know, we can add the custom filtered view in the lookup by using addCustomView in the JavaScript. We can also see a lot of sample scripts online to use that method. All of the scripts that I've gone through need to provide fetchXml and layoutXml.
I also used to code the same way and get the fetchXml by downloading from Advanced Find and layoutXml by using View Layout Replicator of XRM Toolbox. The problem with this method is that the columns are hardcoded in the script and the customization changes to the views are no longer applicable.
So, I just modified the script in order to add the custom filtered view based on the current default view of the lookup which also saves some development time for getting the layout XML. The parameters required for the script are
Please take note that the following script contains one Soap.Retrieve to retrieve the savedquery of the default view so that it may not be recommended if there is a requirement to filter so many lookups.
The script has a dependency on XrmServiceToolkit.js for XrmServiceToolkit.Soap.Retrieve() which again has a dependency on JSON2, jQuery 1.7.2(modified). You may replace the code in the Retrieve part with your webservice Retrieve call from the utility jscript if you have any.
I just added the script for Rollup 11 and below based on the old codes but I haven't tested. Please leave a comment if you have any issue with the script.
I also used to code the same way and get the fetchXml by downloading from Advanced Find and layoutXml by using View Layout Replicator of XRM Toolbox. The problem with this method is that the columns are hardcoded in the script and the customization changes to the views are no longer applicable.
So, I just modified the script in order to add the custom filtered view based on the current default view of the lookup which also saves some development time for getting the layout XML. The parameters required for the script are
- The name of the new view
- Condition and link entity part of the Fetch XML (which you can easily obtained from Advanced Find)
- Lookup attribute logical name
Please take note that the following script contains one Soap.Retrieve to retrieve the savedquery of the default view so that it may not be recommended if there is a requirement to filter so many lookups.
The script has a dependency on XrmServiceToolkit.js for XrmServiceToolkit.Soap.Retrieve() which again has a dependency on JSON2, jQuery 1.7.2(modified). You may replace the code in the Retrieve part with your webservice Retrieve call from the utility jscript if you have any.
//E.g. var conditionXml = '<filter type=\"and\"><condition attribute=\"parentcustomerid\" operator=\"not-null\" /></filter>';
// addCustomFilter("Contacts with Parent Customer", conditionXml, "new_contactid")
//E.g2. var conditionXml = '<link-entity name=\"account\" from=\"accountid\" to=\"parentcustomerid\" alias=\"ab\"><filter type=\"and\"><condition attribute=\"name\" operator=\"eq\" value=\"ABC\" /></filter></link-entity>';
// addCustomFilter("Contacts from ABC Organization", conditionXml, "new_contactid2")
function addCustomFilter(viewDisplayName, conditionXml, lookupAttributeToSet)
{
// Note: in the form designer make sure the lookup field is set to "Show all views" in its "View Selector" property
// Dependency: XrmServiceToolkit.js for XrmServiceToolkit.Soap.Retrieve()
// For Random Guid Generation
function S4(){return (((1+Math.random())*0x10000)|0).toString(16).substring(1);}
// Cache default view fetch xml and view layout xml for subsequent filters
if (window.defaultViewFetchXml == null || window.defaultLayoutXml == null)
{
window.defaultViewFetchXml = new Object();
window.defaultLayoutXml = new Object();
}
if (window.defaultViewFetchXml[lookupAttributeToSet] == null || window.defaultLayoutXml[lookupAttributeToSet] == null)
{
var cols = ["fetchxml", "layoutxml"];
var retrievedView = XrmServiceToolkit.Soap.Retrieve("savedquery", Xrm.Page.getControl(lookupAttributeToSet).getDefaultView(), cols);
window.defaultViewFetchXml[lookupAttributeToSet] = retrievedView.attributes["fetchxml"].value;
window.defaultLayoutXml[lookupAttributeToSet] = retrievedView.attributes["layoutxml"].value;
}
//Set parameters values needed for the creation of a new lookup view...
var viewId; // Your new lookup views needs a unique id. It must be a GUID. Here I use the GUID of the current record.
if (Xrm.Page.data.entity.getId() != null)
{
viewId = Xrm.Page.data.entity.getId();
}
else
{
viewId = "{" + (S4() + S4() + "-" + S4() + "-4" + S4().substr(0,3) + "-" + S4() + "-" + S4() + S4() + S4()).toLowerCase() + "}";
}
var fetchXml = window.defaultViewFetchXml[lookupAttributeToSet];
var layoutXml = window.defaultLayoutXml[lookupAttributeToSet];
//var xmlDoc = $.parseXML(fetchXml);
//$xml = $(xmlDoc);
//var entityName = $xml.find("entity")[0].attributes["name"].value;
var entityName = fetchXml.substring(fetchXml.indexOf("entity name") + 13, fetchXml.indexOf("\"", fetchXml.indexOf("entity name") + 13));
var viewDisplayName = viewDisplayName; // A name for new lookup view
var viewIsDefault = true; // Whether your new lookup view should be the default view displayed in the lookup or not
fetchXml = fetchXml.replace("</entity>", "").replace("</fetch>", "") + conditionXml + "</entity>" + "</fetch>";
//Add your new view to the Lookup's set of availabe views and make it the default view
Xrm.Page.getControl(lookupAttributeToSet).addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, viewIsDefault);
Xrm.Page.getControl(lookupAttributeToSet).setDefaultView(viewId);
// Disabling filter lookup view selector
if (document.getElementById(lookupAttributeToSet).disableViewPicker == null) // For Rollup 12 and above
{
document.getElementById(lookupAttributeToSet)._behaviors[0].AddParam("DisableViewPicker", "1");
document.getElementById(lookupAttributeToSet)._behaviors[0].set_showProperty(1);
}
else // For Rollup 11 and below
{
document.getElementById(lookupAttributeToSet).disableViewPicker = 1;
}
// Hiding New Button in filter lookup dialog
try // For Rollup 12 and above
{
document.getElementById(lookupAttributeToSet)._behaviors[0].AddParam("ShowNewButton", "0");
document.getElementById(lookupAttributeToSet)._behaviors[0].set_showProperty(0);
}
catch(err) // For Rollup 11 and below
{
crmForm.all[lookupAttributeToSet].AddParam("ShowNewButton", "0");
}
return fetchXml; // Just for debugging purpose
}
I just added the script for Rollup 11 and below based on the old codes but I haven't tested. Please leave a comment if you have any issue with the script.
Comments
Post a Comment