Filter 1:N Add Existing Lookup Dynamics

Recently, I had a requirement that in the Account form when a user clicks the Add button in the Child Accounts subgrid which has 1:N relationship, the user want to view and select accounts without parent in the lookup view.

I know this is not possible to configure OOTB, and I once achieved similar functionality with N:N relationship following this post from Paul Nieuwelaar. Unfortunately, there is no post (or I missed it somewhere while doing research) how to do it with 1:N relationship subgrid, so I had to figure it out myself based on the N:N approach.

After a few hours digging into the system Javascript code, I successfully made it work. I think I should blog this to help others. Please note that because I’m following the N:N post which is applied to CRM 2016 turbo forms, my code also works for CRM 2016 and probably for Dynamics CRM / 365 CE (haven’t tested). For earlier versions, you should follow Paul’s posts for 2013 and 2011, and apply changes where needed.

Let’s get to the fun part. Basically, we need to change the behavior of the “Add” button of the subgrid by passing a custom view to it instead of using the OOTB lookup view.

First, we need to download and install Ribbon Workbench solution to our CRM.

Second, create a Javascript web resource using the below code snippet.

webresourceDownload the code snippet here

Pay attention to the highlighted parts. They are where I made changes compared to Paul’s post.

Third, create a custom solution and include: the Account entity, Application Ribbons, and our javascript web resource. And remember to backup the solution so we can rollback if something goes wrong.

customizeribbonsolution.PNG

Now open the Ribbon Workbench:

  1. Select our solution to begin customizing the Ribbon.ribbon1
  2. Select the Account entity
    • ribbon2
  3. Locate the Add button in the SubGrid area. There are actually three commands in place of the Add icon:ribbon3
    1. ADD NEW {0}: this one is used when the relationship is 1:N and the primary entity lookup field is “Business Required”.
    2. the first ADD EXISTING {0} (AddExistingStandard): this is one is used when the relationship is 1:N and the primary entity lookup field is not required. We’ll customize this one in our case.
    3. the second ADD EXISTING {0} (AddExistingAssoc): this is one is used when the relationship is N:N
  4. Right click the ADD EXISTING {0} (AddExistingStandard, usually the first one), select Customize Command ribbon4
  5. In the bottom panel, expand the COMMANDS group, select the Mscrm.AddExistingRecordFromSubGridStandard
  6. In the right panel, we will update the javascript action to point to our function “filterAddExistingAccount” in our web resource.
  7. Add an additional Crm Paremeter by clicking Add Parameter button > CRM Parameter, from the dropdown menu select PrimaryEntityTypeName.ribbon567.png
  8. Click Publish button on the top menu bar.

That’s it. Now refresh your CRM, navigate to Accounts and open an Account, from the Child Accounts subgrid click Add button, a popup dialog should open with our custom filtered views.

Note: Filtering the Add Existing view like this is unsupported, so there is no guarantee the next major update won’t break this again. But until then, enjoy!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s