Counting many-to-many records, how can I do this with plugins? RRS feed

  • Question

  • Hello Experts,

    I have a question. I have a N:N (many-to-many) relationship between Lead and a Custom Entity called "Sales Referrals". I need to count the total number of Leads a Sales Referral may have. I am trying to do this via a plugin. I am familiar in counting records when there is a field but have never tried to count when there is a many-to-many relationship. Does anyone have any advice or code snippets?

    Sunday, December 8, 2013 7:46 PM

All replies

  • Hello,

    You can use aggregation through FetchXml. Then develop plugin which will handle Associate/Diassociate messages, calculate totals and update Sales Referral entity with count of associated leads.

    Dynamics CRM MVP/ Technical Evangelist at SlickData LLC
    My blog
    Sunday, December 8, 2013 8:05 PM
  • Hi,

    As Proposed by Andrii, if you use fetchxml instead of query expression, you will get the count of N:N related records. Count of N:N related records can be obtained from the resultset of the fetchexpression.

    Kind Regards,


    Monday, December 9, 2013 5:07 AM