Wednesday 5 December 2012

Filterred Lookup in MSCRM 2011

Hi Folks,

Filttered Lookup in MSCRM 2011

function ChangeLookupView_Customer() {
//Note: in the form designer make sure the lookup field is set to

//"Show all views" in its "View Selector" property 

//Set parameters values needed for the creation of a new lookup view...

// Your new lookup views needs a unique id.  It must be a GUID.  Here I use the GUID of the current record.
var viewId = Xrm.Page.data.entity.getId();     
// The entity your new lookup view relates to
var entityName = "account";
// A name for new lookup view                   
var viewDisplayName = "Account - Customers";
// Whether your new lookup view should be the default view displayed in the lookup or not
var viewIsDefault = true;                      
   
//Define the Fetch XML query your new lookup view will use.  You can create this via Advanced Find.  You'll need to massage the syntax a little though
var fetchXml = 
              "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
              "<entity name='account'>" +
              "<attribute name='name' />" +
              "<attribute name='accountid' />" +
              "<attribute name='customertypecode' />" +
              "<filter type='and'>" +
              "<condition attribute='customertypecode' operator='eq' value='3' />" +
              "</filter>" +
              "<order attribute='name' decending='false' />" +
              "</entity>" +
              "</fetch>";

//Define the appearance of your new lookup view
    var layoutXml = 
                    "<grid name='resultset' object='1' jump='name' select='1' icon='1' preview='1'>" +  
                    "<row name='result' id='accountid'>" + 
// id = the GUID field from your view that the lookup should return to the CRM form
                    "<cell name='name' width='200' />" +  
                    "<cell name='customertypecode' width='200' />" +
                    "</row>" +
                    "</grid>";

    //Add your new view to the Lookup's set of availabe views and make it the default view
Xrm.Page.getControl("new_customer").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, viewIsDefault);
}


function ChangeLookupView_Supplier() {

//Set parameters values needed for the creation of a new lookup view...
    var viewId = Xrm.Page.data.entity.getId(); 
// Using the same GUID, the viewId only has to be unique within each lookup's set of views
    var entityName = "account";
    var viewDisplayName = "Account - Suppliers";
    var viewIsDefault = true;

    //Define the Fetch XML query your new lookup view will use  
    var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
          "<entity name='account'>" +
          "<attribute name='name' />" +
          "<attribute name='accountid' />" +
          "<attribute name='customertypecode' />" +
          "<filter type='and'>" +
// A different value provided here to produce a different result set
          "<condition attribute='customertypecode' operator='eq' value='10' />" +  
          "</filter>" +
          "<order attribute='name' decending='false' />" +
          "</entity>" +
          "</fetch>";

    //Define the appearance of your new lookup view
    //No changes required here compared to the above, the lookups should have the same appearance
    var layoutXml = "<grid name='resultset' object='1' jump='name' select='1' icon='1' preview='1'>" +  
             "<row name='result' id='accountid'>" +
              "<cell name='name' width='200' />" +
              "<cell name='customertypecode' width='200' />" +
             "</row>" +
            "</grid>";

    //Add your new view to the Lookup's set of availabe views and make it the default view
    //The supplier field is specified here, we want to add this view to that lookup field
 Xrm.Page.getControl("new_supplier").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, viewIsDefault);
}


 
Thanks to Gareth Tucker 

4 comments:

  1. Hi nandan,

    I would want to ask if it reffers also to the subgrids, I mean is it useful when I want to add custom view into the subgrid type control on the form?

    ReplyDelete
  2. Yes okube u can even create ur own custom view using fetch XML and set it to subgrid control also

    ReplyDelete
  3. I know about creating my own fetchXml, and setting it into subgrid, but the bad thing is that when you want to filter that subgrid from GUI, it takes input data from default view of the subgrid, not the data you show by setting custom fetchXml.

    Thats why I am asking about setting whole view into the subgrid, not only custom fetchXml :)

    ReplyDelete
  4. For that you need to set ur Custom view as ur default view

    ReplyDelete