Saturday, July 9, 2011

Custom Lookup Filter Lookup in CRM 2011 using javascript

Hi All, Some times we will get requirement to set filterlookup using java script. CRM 2011 provides OOB Filter Lookups, but it has some limitations. For Activities they did not provided filter lookup facility. Recently i came accross Letter activity to set filter lookup. Here is the way to set filter lookup  using java script.

Here I am going to set contact lookup with selected account as parent.


function customfilterlookup(AccoundID)
{
//Show Contacts which has selected parent Account
    //build fetchxml, use Advance Find to get Fetchxml
    var viewId = "{a76b2c46-c28e-4e5e-9ddf-951b71202c9d}"; //view Guid
    var entityName = "contact"; // Entity to be filtered
    var viewDisplayName = "Active Contacts"; // Custom name for the lookup window.
    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
                  "<entity name='contact'>" +
                    "<attribute name='fullname' />" +
                    "<attribute name='parentcustomerid' />" +                  
                    "<attribute name='emailaddress1' />" +
                    "<attribute name='address1_telephone2' />" +
                    "<attribute name='new_city' />" +
                    "<attribute name='address1_stateorprovince' />" +
                    "<attribute name='address1_telephone1' />" +
                    "<attribute name='ownerid' />" +
                    "<attribute name='contactid' />" +
                    "<order attribute='fullname' descending='false' />" +
                    "<filter type='and'>" +
                      "<condition attribute='parentcustomerid' operator='eq'  uitype='account' value='"+AccoundID+"' />" +
                      "<condition attribute='statecode' operator='eq' value='0' />" +
                    "</filter>" +
                  "</entity>" +
                "</fetch>";
    // Build Grid Layout. building a custom view for the Lookup
    //building grid layout with the columns which needs to be displayed in the lookup view
    var layoutXml = "<grid name='resultset' " +
                    "object='1' " +
                    "jump='name' " +
                    "select='1' " +
                    "icon='1' " +
                    "preview='1'>" +
                    "<row name='result' " +
                    "id='contactid'>" + // Id/key attribute of the entity to be filtered
                    "<cell name='fullname' " +
                    "width='250' />" +
                    "<cell name='new_city' " +
                    "width='70' />" +
                    "<cell name='address1_telephone1' " +
                    "width='100' />" +
                    "</row>" +
                    "</grid>";
    // add new view to the lookup
    Xrm.Page.getControl("contact").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
}

keep smiling.. :)

2 comments:

  1. Dear Guru Prasad,

    I like to have the result of the fetch statement in a variable. Do you know to do that?

    Thanks,

    Marco

    ReplyDelete
  2. Hi Marco,
    go through the link,
    http://www.crmsoftwareblog.com/2011/05/execute-fetch-from-javascript-in-crm-2011/

    hope it will help!!!!

    ReplyDelete