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.
- function filterConnectionRoles() {
- try {
- // Identify Connecting From Entity
- var connectfromEntity = Xrm.Page.getAttribute("record1id").getValue()[0].typename;
- // Construct oData query to retrieve list of associated Connection Roles
- var oDataEndpoint = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc";
- var oDataQuery = oDataEndpoint + "/ConnectionRoleObjectTypeCodeSet?$filter=AssociatedObjectTypeCode eq '"
- + connectfromEntity + "'&select=ConnectionRoleId";
- // Execute oData Query
- $.ajax({
- type: "GET",
- contentType: "application/json; charset=utf-8",
- datatype: "json",
- url: oDataQuery,
- beforeSend: function (XMLHttpRequest) {
- XMLHttpRequest.setRequestHeader("Accept", "application/json");
- },
- success: function (data, textStatus, XmlHttpRequest) {
- if (data != null) {
- // Build the condition values
- var conditionValues = "";
- for (var i = 0; i < data.d.results.length; i++) {
- conditionValues += "<value>" + data.d.results[i].ConnectionRoleId.Id + "</value>";
- }
- if (conditionValues.length > 0) {
- // Construct Fetch Query
- var viewId = "{E3319041-5017-4353-A578-AEC2CCF28DA8}";
- var viewDisplayName = "Connection Roles";
- var fetch = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
- "<entity name='connectionrole'>" +
- "<attribute name='name' />" +
- "<attribute name='connectionroleid' />" +
- "<order attribute='name' descending='false' />" +
- "<filter type='and'>" +
- "<condition attribute='connectionroleid' operator='in' >" +
- conditionValues +
- "</condition>" +
- "</filter>" +
- "</entity>" +
- "</fetch>";
- // Construct Grid Layout
- var grid = "<grid name='resultset' object='3231' jump='name' select='1' icon='1' preview='1'>" +
- " <row name='result' id='connectionroleid'>" +
- " <cell name='name' width='200' />" +
- " </row>" +
- "</grid>";
- // Add Custom Lookup View
- $("#record2roleid").attr("disableViewPicker", "0");
- Xrm.Page.getControl("record2roleid").addCustomView(viewId, "connectionrole", viewDisplayName, fetch, grid, true);
- $("#record2roleid").attr("disableViewPicker", "1");
- }
- }
- },
- error: function (XmlHttpRequest, textStatus, errorThrown) {
- alert('oData Error: ' + oDataQuery);
- }
- });
- }
- catch (ex) {
- alert(ex);
- }
- }
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