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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

associate one to many relationship through code

(0) ShareShare
ReportReport
Posted on by 2,510

Hi, I am trying to associate one to many relationship through code, it seems that it is different from many to many association, since I just need add existing id to the relationship entity for many to many association. For one to many, do I have to create the record first? since every record has lookup values, not sure associate this. please help.

*This post is locked for comments

I have the same question (0)
  • Verified answer
    ashlega Profile Picture
    34,477 on at

    Hi,

     you can simply set the lookup attribute on the child entity

     childRecord["lookupattribute"] = new EntityReference(parentEntityName, parentId);

     ...

     Update(childRecord);

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Hi,

    A one to many relationship is basically a lookup. You have to set the value of the lookup. This can be done from either JavaScript or plugin code. The target record in both cases must exist.

    In JavaScript (one of the ways), you can call the following function, and pass the parameters as required:

    // fieldname: the attribute that you want to set

    // lookupid: the unique identifier of the relationship

    // lookupname: the text of the identifier

    // entityname: the logical name of the entity

    function setLookupField(fieldName, lookupId, lookupName, entityName) {

       var lookupData = new Array();

       var lookupItem = new Object();

       lookupItem.id = lookupId;

       lookupItem.name = lookupName;

       lookupItem.entityType = entityName;

       lookupData[0] = lookupItem;

       Xrm.Page.getAttribute(fieldName).setValue(lookupData);

    }

    In C#/Plugin code

    entity[lookupFieldName] = new EntityReference(id_of_lookup, targetEntityName);

    Hope this helps.

  • sdnd2000 Profile Picture
    2,510 on at

    sorry, I am bit confused, here is my scenario, I have incident and contact 1: n relationship. how can I set the contact in a new case, I have to use c# plugin code.

  • Verified answer
    ashlega Profile Picture
    34,477 on at

    case["contactattribute"] = new EntityReference("contact", contactid);

    And you can only do it once they click "save" (since it's a plugin)

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans