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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

how to get values from a N:1 relationship record using Javascript

(0) ShareShare
ReportReport
Posted on by 547

Dear all,

I have a entity B where in every Records has realtionship to entity A's some record i.e N:1 entity.

My requirement is when I Click on Save for Entity b's record I need to get either GUID or any Reference value of realted record (N:1) from Entity A.

I need to do this with Javascript. Any Idea?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    If I understand correctly, many B has a lookup to A? N B : 1 A? If that's the case, then just grab the [0].id value of the lookup to A.

    See mehmetgunen.com/get-set-lookup-field-value-using-javascript-in-crm how to get id from lookup.

  • Suggested answer
    Community Member Profile Picture
    on at

    As u said there is N:1 relationship between Entity B and Entity A. So you can directly read the value like :

    var lookupObject = Xrm.Page.getAttribute("yourlookupattributename");

      if (lookupObject != null)

    {

          var lookUpObjectValue = lookupObject.getValue();

          if ((lookUpObjectValue != null))

          {

           var lookuptextvalue = lookUpObjectValue[0].name;

           var lookupid = lookUpObjectValue[0].id;

          }

    }

  • chikhaleankush Profile Picture
    547 on at

    Thanks Erik for your reply.

    I believe yes you understood the requirement correctly.

    the answer you suggested, I have question regarding it, what do you mean by lookup?

    I mean I have no lookup filed in my entity B for entity A. This is the direct relationship.

    The link you refereed, If I am not wrong is for lookup field and get its value which I know and I frequently use it. But again here no Lookup field.

    What do you say?

    Regards,

    Ankush

  • chikhaleankush Profile Picture
    547 on at

    Thanks Rahul for your reply. I have no lookup field in my Entity B's from. I have direct relationship built on Entity A and hence for B it comes as N:1

    If there was a lookup field it would have been very easy but without that any way to get values or at least GUID if attached  or not!!

    Regards,

    Ankush

  • Community Member Profile Picture
    on at

    In the case that you don't have a lookup on the form for the relationship, I would use fetchXML to retrieve the id.

    Perhaps you could add a lookup, and have it hidden on the form? You could still access it by code.

    If you need to use fetchXML, here is a thorough guide:

    arunpotti.wordpress.com/.../retrieve-records-using-fetch-xml-java-script-in-crm-201113

    Maybe I misunderstand, and the lookup could be on A (for B) instead?

  • Suggested answer
    Aman Kothari Profile Picture
    on at

    Hi,

    You can also get with Odata query like below code.

    E.g.

    For getting contact related to account (N:1 relation between account and contact)

    var req = new XMLHttpRequest();

    req.open("GET", encodeURI(Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/AccountSet(guid'AccountGuid')?$select=account_primary_contact/contactAttribute1,account_primary_contact/contactAttribute2&$expand=account_primary_contact"), true);

    req.setRequestHeader("Accept", "application/json");

    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");

    req.onreadystatechange = function () {

       if (this.readyState === 4) {

           req.onreadystatechange = null;

           if (this.status === 200) {

               var result = JSON.parse(req.responseText).d;

               var account_primary_contact_FirstName = result.account_primary_contact.FirstName;

               var account_primary_contact_FullName = result.account_primary_contact.FullName;

           }

           else {

               alert(this.statusText);

           }

       }

    };

    req.send();

    Hope it'll help you.

    Thanks

  • Verified answer
    Mahendar Pal Profile Picture
    45,095 on at

    Hi Ankush,

    When you setup 1:N relationship between two entities, for example A and B having 1:N relationship, so you will get lookup controls at N side of entity in our case entity B will get lookup field as result of relationship and entity A will get associated view as result of relationship which will display list of the records which is associated.

    If there is no association you could write retrievemultiple call to get data from your entity based on some condition, for example may based on city. If you will check SDK you see sample code there for retrievemultiple.

    Hope it will help you.

    Thanks

  • chikhaleankush Profile Picture
    547 on at

    Hi Erik,

    Thanks for your reply.As you suggested using XML there is similar "CrmServiceToolkit"

    which I always use for fecthing using query.I could get other simple data using my query but I could not get the ID as Id of my entity is saved as below

    4405.sample.png

    as you see if I simply use xyz_CasesId    I could not fetch the details of it as it has ID,Name and entity attached to it. Any Idea how can I use it.

    below is query 

    var query =
    "<q1:EntityName>CasesId.Id</q1:EntityName>" +
    "<q1:ColumnSet xsi:type='q1:ColumnSet'>" +
    "<q1:Attributes>" +
    "<q1:Attribute>CreatedBy </q1:Attribute>" +
    "</q1:Attributes>" +
    "</q1:ColumnSet>" +
    "<q1:Distinct>false</q1:Distinct>";

    var retrievedContacts = CrmServiceToolkit.RetrieveMultiple(query);

  • chikhaleankush Profile Picture
    547 on at

    Hi Mahen,

    Thanks for  your reply.Yes I agree but what system is here Entity A will have just simply attached the entity and chooses the record which they wish to from entity B.

    Now my work is in Entity B to check if there is any other entity's record added & if it does fetch the data from other Entitys data. yes you suggested it correct if there was a lookup my work would have been simple but unfortunately not. I will attach the screen shot for your Reference. I hope you get it.

    entry you see is Entity B and the frame is for Entity A and this is how user links 2 entities no lookup

    sample2.png

  • chikhaleankush Profile Picture
    547 on at

    Hi Mahen,

    yes you were right,I get the lookup filed and the value is automatically populated in it.Once I have a lookup filed on my form,rest is easy. But It took time for us to figure out.

    Thank you every one for the help :)

    Regards,

    Ankush

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans