CRM 2011- Creating a Custom Filtered Lookup

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

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...