Tag Archives: fetchxml

How To – Series 1: How to add/change filter criteria of a Lookup field in Java Script

Hi all… This is my first blog post in How-To Series.

Let’s consider a scenario, where we have three picklist fields on a form and the Lookup filed search criteria is depended on selected picklist values. For an instance, if we have Account Type, Product Type and Call Type as picklists and the requirement is like by the time user clicks on the Lookup filed it should have below as the filter criteria

1. Include only the picklists whose value is selected
2. If none of the picklist is selected then display all active records

Below is the code which needs to be placed in the onLoad of the crmForm. As the fetchxml code needs to be changed as per the user picklist selection, I have declared four variables named param, headparam, tailparam and bodyparam. In which bodyparam content is changing as per the user selection. Finally I am concatenating headparam, bodyparam and tailparam to generate required fetchxml code. I am implementing entire code as a function so that it can be called from the picklist’s onchange events instead copy and pasting entire code each and every place.

crmForm.lookupquery = function xyz() {
var field = crmForm.all.abaxis_lookupid;
    field.lookupbrowse = 1;
    var headparam = “<fetch mapping=’logical’><entity name=’new_myentity’><all-attributes /><filter type=’and’>”;
    var tailparam = “</filter></entity></fetch>”;
    var bodyparam = “”;
    var param = “”;
    if (crmForm.all.new_plproducttype.DataValue != null)
        bodyparam = bodyparam + “<condition attribute=’new_producttype’ operator=’eq’ value='” + crmForm.all.new_plproducttype.DataValue[0].id + ” ‘/>”;
    if (crmForm.all.new_placcounttype.DataValue != null)
        bodyparam = bodyparam + “<condition attribute=’new_accounttype’ operator=’eq’ value='” + crmForm.all.new_placcounttype.DataValue + ” ‘/>”;
    if (crmForm.all.new_plcallfor.DataValue != null)
        bodyparam = bodyparam + “<condition attribute=’new_callfor’ operator=’eq’ value='” + crmForm.all.new_plcallfor.DataValue + ” ‘/>”;
    if (bodyparam == “”)
        bodyparam = “<condition attribute=’statecode’ operator=’eq’ value=’0’/>”;
    param = headparam + bodyparam + tailparam;

    field.AddParam(“search”, param);

 Add crmForm.lookupquery(); to all the picklist attributes onChange() event so that as soon as a picklist value is changed lookup field filter criteria will also be changed.

Hope it helps you..!!!!