Filter N:N Add Existing Lookup Dynamics CRM 2013

Paul Nieuwelaar, 21 April 2014

In a previous blog post of mine I showed how to filter the N:N Add Existing button in CRM 2011. Since the upgrade to CRM 2013, this no longer works. Given that the code was unsupported, this was expected. Since CRM 2013 has changed the UI, the method in which N:N’s work has also changed.

The old code relied on a CRM function ‘LookupObjects’, which returned the records the user selected from the lookup. Since 2013 now uses ‘light-box’ pop-ups, rather than browser pop-ups, the JavaScript cannot be halted while the user selects their records. Because of this, CRM now uses callback functions to continue processing after the user has selected records from the lookup.

The new function is called ‘LookupObjectsWithCallback’, and is exactly the same as the old function, except that it takes a callback function as the first parameter. I’ve pulled apart the CRM functions, and reconstructed them into my own function, which allows us to pass in a custom view as the default.

The new updated code is below:

//filters an add existing lookup view (N:N)
function addExistingFromSubGridCustom(gridTypeCode, gridControl, context, fetch, layout, viewName) {
    var viewId = "{1DFB2B35-B07C-44D1-868D-258DEEAB88E2}"; // a dummy view ID
    var relName = gridControl.GetParameter("relName");
    var roleOrd = gridControl.GetParameter("roleOrd");
  
    //creates the custom view object
    var customView = {
        fetchXml: fetch,
        id: viewId,
        layoutXml: layout,
        name: viewName,
        recordType: gridTypeCode,
        Type: 0
    };
  
    var parent = GetParentObject(null, 0);
    var parameters = [gridTypeCode, "", relName, roleOrd, parent];
    var callbackRef = Mscrm.Utilities.createCallbackFunctionObject("locAssocObjAction", context, parameters, false);
  
    //pops the lookup window with our view injected
    LookupObjectsWithCallback(callbackRef, null, "multi", gridTypeCode, 0, null, "", null, null, null, null, null, null, viewId, [customView]);
}
  
//filters the Contact N:N lookup view from Account to show only Pauls!!
function filterAddExistingContact(gridTypeCode, gridControl, primaryEntity) {
    if (primaryEntity != "account") {
        Mscrm.GridRibbonActions.addExistingFromSubGridAssociated(gridTypeCode, gridControl); //default button click function
        return;
    }
  
    //fetch to retrieve filtered data
    var fetch = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
        "  <entity name='contact'>" +
        "    <attribute name='fullname' />" +
        "    <order attribute='fullname' descending='false' />" +
        "    <filter type='and'>" +
        "      <condition attribute='statecode' operator='eq' value='0' />" +
        "      <condition attribute='firstname' operator='eq' value='Paul' />" +
        "    </filter>" +
        "  </entity>" +
        "</fetch>";
  
    //columns to display in the custom view (make sure to include these in the fetch query)
    var layout = "<grid name='resultset' object='1' jump='contactid' select='1' icon='1' preview='1'>" +
        "  <row name='result' id='contactid'>" +
        "    <cell name='fullname' width='300' />" +
        "  </row>" +
        "</grid>";
  
    addExistingFromSubGridCustom(gridTypeCode, gridControl, this, fetch, layout, "Filtered Contacts");
}

The changes are mostly to the first function, however the second function has also been updated to pass 'this' into the other function. For this example, I have created a N:N between Account and Contact, and am filtering the ‘Contact’ view from the Account side to show only contacts with a first name of ‘Paul’. 



For step by step instructions on how to customize the ribbon to get this working, check out the 2011 post, which has not changed:
http://www.magnetismsolutions.com/blog/paulnieuwelaar/2013/02/04/filter-n-n-add-existing-lookup-dynamics-crm-2011-rollup-12

You will also need the Ribbon Workbench for CRM 2013.

Note that this method uses the old popup window where you select multiple records at once, rather than the new CRM 2013 inline lookup where you select 1 at a time (personally I prefer the pop-up). This functionality is still unsupported, and is likely to break with any major updates.