Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

How to get parent record ID while adding a new record in n:n relationship

Posted on by 95

suppose i have a 'account' entity and i have a grid of 'contact' in account form such that account and contact have n:n relationship.

now if i add a new contact using the grid of contacts in account form then how can i get the customer record ID using C# plugin. Please tell.

*This post is locked for comments

  • Suggested answer
    shivaram Profile Picture
    shivaram 3,315 on at
    RE: How to get parent record ID while adding a new record in n:n relationship

    Hi,

    when ever you create any N:N Relationship, then CRM DB will create one table with the name of Schema name of N:N Relationship.Based on that you need to retrieve GUID.

    for example Account to Contact N:N Relationship is name is new_account_contact

     

    Then I will retrieve record like

    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    {
    Entity entity = (Entity)context.InputParameters["Target"];

    using (var xrm = new XrmServiceContext(service))
    {

    var que =Xrm.new_account_contact.where(ac=>ac.accountid==entity.id).select(ca=>ca.contactId).FirstOrDefault();
    }

    }

    Note : Here I am using Earlybind and added XRM file.

    accountid and contactId are columns in new_account_contact table

    Hope it Helps you

    Thanks.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans