web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Forums / Sales forum / Associate accounts wit...
Sales forum

Associate accounts with multiple accounts

(0) ShareShare
ReportReport
Posted on by 90

Hi,

I need to associate accounts with other accounts. The other accounts being associated are called Distributors. The distributor accounts are those accounts that have Relationship Type of Distributor. All these accounts including distributors need to have a parent account. This is kind of many to many relationship between accounts. Below are 3 different approaches that I have tried. 

1- First, I added an account sub-grid (1:N) to the account but that prevents the user the ability to add accounts to the sub-grid if they already have a different parent account.

2- Then I added the OOB Connection to the account and that works fine (out of box many to many relationship) but I need to filter the "Name" lookup when clicked to only show Distributors. I added a fetchxml filter to the lookup but when clicked it shows the 10 first accounts and then if I click on "View more records" it filters correctly. If I cancel out and click the lookup again, it shows the contacts associated with the filtered accounts as well which I don't want. I just want the Distributor accounts to show. 

3- I also tried Scott's Connection UI Refresher but couldn't figure out how to filter his inline "AddNew" lookup button to show only Distributors. 

I have read and tried hundreds of blogs online to no avail for the past 2 days. I really appreciate it if you have any suggestions.

Look forward to your response. 

Thank you so much in advance. 

I have the same question (0)
  • Verified answer
    Drew Poggemann Profile Picture
    4 on at
    RE: Associate accounts with multiple accounts

    Hi,

    I think I would go with an N-N relationship on the Account entity with the Account entity.  This will allow you to add a sub-grid to the entity where you will only select the Distributors, not add new distributors like you would see in a 1-N grid.  You will get a "+" sign on the grid to select Distributors that exist.  I tried this in a test environment just now and I was able to add the same distributor to multiple accounts and multiple distributors to the same account.  Example is I had Account A with 2 distributors (1 and 2) and Account B with 2 distributors (1 and 3).

    We had a similar requirement around a pre-filter but it was for limiting the contacts shown to tie only to the contacts related to an Account.  The logic we added to the Contact pre-filter, which will hopefully help you in this situation...

    function preFilterContact() {
    Xrm.Page.getControl("vca_primarycontact").addPreSearch(function () {
    addContactFilter();
    });
    }

    function addContactFilter() {
    var lookup = Xrm.Page.getAttribute("vca_account").getValue();

    if (lookup && lookup[0] && lookup[0].id) {

    var lookupId = lookup[0].id.substr(1, 36);

    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/contacts?$select=contactid&$filter=statecode eq 0 and _parentcustomerid_value eq " + lookupId, false);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
    req.onreadystatechange = function () {
    if (this.readyState === 4) {
    req.onreadystatechange = null;
    if (this.status === 200) {
    var results = JSON.parse(this.response);
    var filterValues = "";
    for (var i = 0; i < results.value.length; i++) {
    var contactid = results.value[i]["contactid"];
    filterValues += "<value>" + contactid + "</value>";
    }

    var fetchXml = '<filter type="and">' +
    '<condition attribute="contactid" operator="in">' + filterValues + "</condition>" +
    '</filter>';
    Xrm.Page.getControl("vca_primarycontact").addCustomFilter(fetchXml, "contact");
    }
    else {
    alert(this.statusText);
    }
    }
    };
    req.send();
    }
    }

    Another article, I assume you reviewed looking at so many as you discussed:  https://community.dynamics.com/crm/b/akmscrmblog/archive/2016/12/02/add-custom-fetch-xml-to-subgrid-lookup 


    Hope this helps!

  • HomayounAugust Profile Picture
    90 on at
    RE: Associate accounts with multiple accounts

    Hi Drew,

    I can't thank you enough. I was expecting to receive an email when someone responded to my post but never received an email. Just came back to my posting to double check to make sure nobody has responded and saw this. I already had the code when I initially used the 1:N relationship on the account but that caused circular reference when the distributor had a parent. I am pretty sure at some point I tried the N:N relationship but I think I used the wrong entity in the sub-grid. Anyways, I just added a N:N relationship and used my existing code and it is working. You made my day. Thank you so much. Wish you all the best and look forward to your helps in the future :-) Thank you!

  • HomayounAugust Profile Picture
    90 on at
    RE: Associate accounts with multiple accounts

    Hi Drew,

    Sorry to bother you. My sub-grid was working perfectly last time I checked. Today, I was working on something unrelated but somehow noticed that when I select a record in the Distributor sub-grid the delete button will not show. When I add a new n:n sub-grid to another entity, I get the delete button. I deleted the relationship (account to account) and recreated it. I removed the sub-grid and created a new one but still the same. Any ideas what is happening?

    Thanks so much,

    August

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Sales

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans