Create custom filtered lookup to filter records based on start and end date in ms crm 2011


MS CRM 2011 provides us facility to create custom filtered lookup on the fly. So in this post I am going to explain how we can create a custom filtered lookup based on start and end date. Let say we have two entities Entity A and Entity B, having relationship N:1 (A:B) and entity A have two attributes start Date and End Date  and Entity B have a issuedate field. you can below code to create a custom filter lookup view at runtime to show only issuedate between Start Date and End Date.

function SetFilterLookuValues(StrtDate,EndDate) {

 

      //formate date

        StartDate = SDate.getFullYear();

        var M = (parseInt(SDate.getMonth()) + 1);

        StartDate = StartDate + “-” + (M < 10) ? ‘0’ + M : M;

        StartDate = StartDate + “-” + SDate.getDate();

 

        var EDate = new Date(EndDate);

        M = (parseInt(EDate.getMonth()) + 1);

        EndDate = EDate.getFullYear();

        EndDate = EndDate + “-” + (M < 10) ? ‘0’ + M : M;

        EndDate = EndDate + “-” + EDate.getDate();

 

var viewId = “{65EC9B45-EE81-4F89-BAF6-E8603FF8E1E4}”; //Give a unique GUID to view ID

var entityName = “EntityB”; //Name of the entity for lookup filter

var viewDisplayName = “Give View Name Here”;

//Write Fetchxml to fetch data

 

var fetchXml = “<fetch distinct=’true’ mapping=’logical’

output-format=’xml-platform’ version=’1.0′>” +

“<entity name=’EntityB >” + //change entity name here

“<attribute name=’EntityBid’/>” + //change with id attribute (key field)

“<attribute name=’name’/>” +

“<order descending=’false’ attribute=’name’/>” +

“<filter type=’and’>” +

//put filter condition here

“<condition attribute=’issuedate’ value='” + StartDate + “‘ operator=’ge’/>” +

“<condition attribute=’issuedate’ value='” + EndDate + “‘ operator=’le’/>” +

“</filter> </entity> </fetch>”;

//build grid layout for the custom view

var layoutXml = “<grid name=’resultset’ ” +

  “object=’1′ ” +

  “jump=’name’ ” +

  “select=’1′ ” +

  “icon=’1′ ” +

  “preview=’1′>” +

  “<row name=’result’ ” +

  “id=’EntityBid’>” + //id(key) attribute from entityB

 “<cell name=’name’ ” +

  “width=’300′ />” +

  “</row>” +

  “</grid>”;

 

 //add new view view

Xrm.Page.getControl(“LookupfieldName”).addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);

    }

 

 

}

Leave a comment

Filed under MS CRM 2011

Leave a comment