Friday, 16 August 2013

CRM 2011 Connections - How to filter Connection Roles?

I’m not going to explain the uses of Connections within CRM 2011, I think we are all aware of their benefits by now.

This post is to share my opinion of the default behavior of the Connection entity form, whereby the "As this role" look up field displays all the Connection Roles defined, where in comparison I with many others believe it should by default filter to only those for the specific Entity.

Example Solution
You could potentially create some Views, but you could end up with quite a few, depending on the number of entities you are using Connections with.

In this case I prefer to use a Custom Lookup, the main benefits are that the user doesn't have to select the view, as these are defaulted to what we set them too and eliminates room for error.

For the purpose of this example, the following code filters the look up based on which Roles are applicable to the connecting from entity type.

  1. function filterConnectionRoles() {
  2.   try {
  3.     // Identify Connecting From Entity
  4.     var connectfromEntity = Xrm.Page.getAttribute("record1id").getValue()[0].typename;
  5.  
  6.     // Construct oData query to retrieve list of associated Connection Roles
  7.     var oDataEndpoint = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc";
  8.     var oDataQuery = oDataEndpoint + "/ConnectionRoleObjectTypeCodeSet?$filter=AssociatedObjectTypeCode eq '"
  9.         + connectfromEntity + "'&select=ConnectionRoleId";
  10.  
  11.     // Execute oData Query
  12.     $.ajax({
  13.       type: "GET",
  14.       contentType: "application/json; charset=utf-8",
  15.       datatype: "json",
  16.       url: oDataQuery,
  17.       beforeSend: function (XMLHttpRequest) {
  18.         XMLHttpRequest.setRequestHeader("Accept", "application/json");
  19.       },
  20.       success: function (data, textStatus, XmlHttpRequest) {
  21.         if (data != null) {
  22.  
  23.           // Build the condition values
  24.           var conditionValues = "";
  25.           for (var i = 0; i < data.d.results.length; i++) {
  26.             conditionValues += "<value>" + data.d.results[i].ConnectionRoleId.Id + "</value>";
  27.           }
  28.  
  29.           if (conditionValues.length > 0) {
  30.             // Construct Fetch Query
  31.             var viewId = "{E3319041-5017-4353-A578-AEC2CCF28DA8}";
  32.             var viewDisplayName = "Connection Roles";
  33.             var fetch = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
  34.                             "<entity name='connectionrole'>" +
  35.                                 "<attribute name='name' />" +
  36.                                 "<attribute name='connectionroleid' />" +
  37.                                 "<order attribute='name' descending='false' />" +
  38.                                 "<filter type='and'>" +
  39.                                     "<condition attribute='connectionroleid' operator='in' >" +
  40.                                     conditionValues +
  41.                                     "</condition>" +
  42.                                 "</filter>" +
  43.                             "</entity>" +
  44.                         "</fetch>";
  45.  
  46.             // Construct Grid Layout
  47.             var grid = "<grid name='resultset' object='3231' jump='name' select='1' icon='1' preview='1'>" +
  48.                 " <row name='result' id='connectionroleid'>" +
  49.                     " <cell name='name' width='200' />" +
  50.                 " </row>" +
  51.             "</grid>";
  52.  
  53.             // Add Custom Lookup View
  54.             $("#record2roleid").attr("disableViewPicker", "0");
  55.             Xrm.Page.getControl("record2roleid").addCustomView(viewId, "connectionrole", viewDisplayName, fetch, grid, true);
  56.             $("#record2roleid").attr("disableViewPicker", "1");
  57.           }
  58.         }
  59.       },
  60.       error: function (XmlHttpRequest, textStatus, errorThrown) {
  61.         alert('oData Error: ' + oDataQuery);
  62.       }
  63.     });
  64.   }
  65.   catch (ex) {
  66.     alert(ex);
  67.   }
  68. }

1 comment:

  1. I know this post is some time ago, I have stumbled across a problem while implementing this way. The view is always defaulting to 'Applicable connection Roles' even after setting setDefaultView on this control. Is there anyway I can default to my custom view I have created as above?

    ReplyDelete

Action Microsoft.Crm.Setup.Common.Analyzer +CollectAction failed. Fatal error during installation

When installing the Srs Data Connection (Microsoft Dynamics CRM Reporting Extensions), you may have experienced the following error: ...