web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :

CRM 2011- Creating a Custom Filtered Lookup

Alessandro Graps Profile Picture Alessandro Graps 2,664

The daily goal was to filter a custom lookup on CRM 2011. The ingredients to do it are:

  • CRM2011
  • Javascript (Script#)
  • FetchXML

First of all I get the fetch XML to filter the lookup:

<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='activityservicetype'>
<attribute name='activityservicetypeid' />
<attribute name='name' />
<attribute name='createdon' />
<order attribute='name' descending='false' />
<filter type='and'>  
<condition attribute='statecode' operator='eq' value='0' />  
<condition attribute='professionalcategory' operator='null' />
</filter>
</entity>
</fetch>

The second step is to create the javascript to filter it:

var _viewId;
 
// FUNCTION: formOnLoad
function formOnLoad() {
 
  _viewId= Xrm.Page.getControl("professionalCategory").getDefaultView();
  setLookup(false);
 
}
 
function setLookup(resetSelection) {
 
    var owner = Xrm.Page.getAttribute("ownerid").getValue();
 
    if ( owner != null )
    {
      var ownerId= owner[0].id;
      var ownerName = owner[0].name;
 
      if ( resetSelection == true )
      {
        // reset old selection for Contact
        Xrm.Page.getAttribute("professionalCategory").setValue(null);
      }
 
      // use randomly generated GUID Id for our new view
      var viewId = "{C0F1DD64-1BF3-450D-BCDE-DF4732DE1606}";
      var entityName = "professionalcategory";
 
      // give the custom view a name
      var viewDisplayName = "All Professional Category by " + ownerName + "";
 

      var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='activityservicetype'>" +
"<attribute name='activityservicetypeid' />" +
"<attribute name='name' />" +
"<attribute name='createdon' />" +
"<order attribute='name' descending='false' />" +
"<filter type='and'>  " +
"<condition attribute='statecode' operator='eq' value='0' />  " +
"<condition attribute='professionalcategory' operator='null' />" +
"</filter>" +
"</entity>" +
"</fetch>";
 
      // build Grid Layout
      var layoutXml = "<grid name='resultset' " +
                               "object='1' " +
                               "jump='activityservicetypeid' " +
                               "select='1' " +
                               "icon='1' " +
                               "preview='1'>" +
                           "<row name='result' " +
                                "id='activityservicetypeid'>" +
                             "<cell name='name' " +
                                   "width='200' />" +
                             "<cell name='createdon' " +
                                   "width='250' />" +
                           "</row>" +
                         "</grid>";
 
      // add the Custom View to the professionalCategory Control
      Xrm.Page.getControl("professionalCategory").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
    }
    else
    {
      // no result.
      Xrm.Page.getControl("professionalCategory").setDefaultView(_viewId);
    }
}

Now I can put the script inside the FormLoad event of the form.

In this script, these Xrm methods provide us powerful methods for manipulating Lookup Controls to provide an additional method of implementing a filtered lookup.
addCustomView Adds an entirely new custom view to the Lookup Control.
setDefaultView Sets the initial default view when a user clicks on the Lookup Control.
getDefaultView Returns the GUID for the initial view when a user clicks on the Lookup Control.

(further detail on the Xrm.Page methods can be found at the following page within the CRM 2011 SDK.)

We can use these methods to build ourselves a simple set of scripting


Archiviato in:Microsoft Dynamics Crm Tagged: CRM, Microsoft Dynamics CRM

This was originally posted here.

Comments

*This post is locked for comments