Search code examples
javascriptdynamics-crmdynamics-crm-2015

Customize Email's regardingobjectid lookup dropdown


I've tried customizing Email's regardingobjectid lookup dropdown, using the following code:

var regardingobjectid = Xrm.Page.getControl("regardingobjectid");
/* The value of viewId is a dummy value, 
   and only needs to be unique among the other available views for the lookup. */
var viewId = '{00000000-0000-0000-0000-000000000001}';
var entityName = 'lu_service';
var viewDisplayName = 'service - custom view';
var fetchXml =
'<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" >' +
  '<entity name="lu_service">' +
     '<attribute name="lu_serviceid" />' +
     '<attribute name="lu_name" />' +
     '<attribute name="createdon" />' +
     '<order attribute="lu_name" descending="false" />' +
   '</entity>' +
'</fetch>';

var lookupService = new RemoteCommand("LookupService", "RetrieveTypeCode");
lookupService.SetParameter("entityName", "lu_service");
var result = lookupService.Execute();
var objectTypeCode = result.ReturnValue;
var layoutXml =
'<grid name="resultset" object="' + objectTypeCode + '" jump="lu_name" select="1" icon="1" preview="1">' +
   '<row name="lu_service" id="lu_serviceid">' +
      '<cell name="lu_name" width="300" />' +
      '<cell name="createdon" width="125" />' +
   '</row>' +
'</grid>';
regardingobjectid.addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);

I thought that this code should do two things:

1. Customize the lookup dropdown, i.e., display the records of the required type (in my example: lu_service) on the dropdown displayed when clicking the lookup field.

2. Add a custom view onto the window opened when clicking the "Look Up More Records" link (listed at the end of the lookup dropdown).

The result is that 1 doesn't work, and 2 works.

My question is: Should've 1 worked? If not, is it possible to achieve this?


Solution

  • For point #1 in 2015 version, you have to use addPreSearch and addCustomFilter to filter out the unwanted entities in the dropdown list. This is the only supported way/workaround. Read more

    For example, the below filter will show lu_service & remove any account from regardingobjectid lookup. Trick is account will not have null accountid.

    var serviceFilter = "<filter type='and'><condition attribute='lu_serviceid' operator='not-null' /></filter>";
    //remove accounts
    var accountFilter = "<filter type='and'><condition attribute='accountid' operator='null' /></filter>";
    Xrm.Page.getControl('regardingobjectid').addCustomFilter(serviceFilter, "lu_service");
    Xrm.Page.getControl('regardingobjectid').addCustomFilter(accountFilter, "account");