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)

How can I access the parent object of an entity in Javascript, CRM 2015?

(0) ShareShare
ReportReport
Posted on by

Hello!

I would like to do what is outlined in this post: https://community.dynamics.com/crm/f/117/t/104899

I've read a lot about this functionality, so I think it can be done, but I'd like to do it in CRM 2015.

For a little more context about what I'm trying to do:

At form load, I'd like to create an if condition on the child record that populates a field on the child record depending on whether a field on the parent record is populated.

*This post is locked for comments

I have the same question (0)
  • Drew Poggemann Profile Picture
    4 on at

    Hi DaveRB97,

    Aiden does a great job answering this in the following response:  community.dynamics.com/.../214931

    Hopefully his examples solve your challenge here.

    Thanks much,

  • Nithya Gopinath Profile Picture
    17,078 on at

    Hi Dave,

    The javascript code below retrieves the parent contact in lead form and populates the first name, last name, telephone and mobile phone fields in the lead form with the values of fields from the contact form.

    function RetrieveParentContactDetails() {
        var ContactId = Xrm.Page.getAttribute('parentcontactid').getValue()[0].id;
        //utilize retrieveRecord method from REST library to get data based on contact id
        SDK.REST.retrieveRecord(
            ContactId,
            'Contact',
            null, null,
            function (contact) {
                //once we have data fill name fields
                Xrm.Page.getAttribute('firstname').setValue(contact.FirstName);
                Xrm.Page.getAttribute('lastname').setValue(contact.LastName);
                Xrm.Page.getAttribute('telephone1').setValue(contact.Telephone1);
                Xrm.Page.getAttribute('mobilephone').setValue(contact.MobilePhone);
            },
    
            function Error() {
                alert('There is error in reading contact data');
            });
    }

    Also, remember to register the SDK.REST.js file in Form Properties.

    Hope this helps.

  • Community Member Profile Picture
    on at

    Hello Nithya,

    Thanks for your response. It looks like that code is moving me in the right direction. I have two questions:

    1) I don't want to "set" values on the form I'm working on. I just want to pull values from the parent into my Javascript as variables and then form logical operations on them. Here's my current (non-working) code:

    function Form_Onload()
    {
      var parentId = Xrm.Page.getAttribute('initiativestepid').getValue()[0].id;
      var testerVariable = null;
        
      // utilize retrieveRecord method from REST library to get data based on parent id
      SDK.REST.retrieveRecord
      (
        parentId,
        'Initiative',
        null, null,
        function (initiative)
        {
          //once we have data fill name fields
          testerVariable = getValue(initiative.estclosedate);
        },
    
        function Error()
        {
          alert('There is error in reading initiative data');
        }
      );
    
      alert(testerVariable);
    }


    How do I change my code to do what I want it to do?

    2) What is "contact" in your code ("initiative" in mine - the parameter passed to the inner function)? I have no idea where that's defined or what it is when it's passed in.

  • Verified answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi Dave,

    1. Just change the code as shown below to achieve your requirement.

    function Form_Onload()
    {
      var parentId = Xrm.Page.getAttribute('initiativestepid').getValue()[0].id;
      var testerVariable = null;
        
      // utilize retrieveRecord method from REST library to get data based on parent id
      SDK.REST.retrieveRecord
      (
        parentId,
        'new_Initiative',
        null, null,
        function (initiative)
        {
          testerVariable = initiative.estclosedate;
          alert(testerVariable);
        },
    
        function Error()
        {      
    alert('There is error in reading initiative data'); } ); }

    2. Contact is the schema name of the parent entity. You should replace the same with the schema name of your parent entity.

       Also, note that estclosedate in the code above is the schema name of this field in your parent entity.

    Hope this helps.

  • Community Member Profile Picture
    on at

    Hello Nithya!

    I just got back to this project today and tried out your code, edited for my system of course.

    Here's my code:

    function Form_Onload()
    {
      var parentId = Xrm.Page.getAttribute('initiativestepid').getValue()[0].id;
      alert(parentId + " 1");
      var testerVariable = null;
        
      // utilize retrieveRecord method from REST library to get data based on parent id
      SDK.REST.retrieveRecord
      (
        parentId,
        'initiatives',
        null, null,
        function (initiatives)
        {
          testerVariable = initiatives.estcompletiondate;
          alert(testerVariable + " 2");
        },
    
        function Error()
        {
          alert('There is error in reading initiative data');
        }
      );
    
      alert(testerVariable + " 3");
    }

    The issue happening now is that the webpage, on loading, spits out the #3 alert first, then #2. I find that strange since the #3 alert is clearly after the retrieveRecord function. Also, the #2 alert simply displays "undefined."

    Any other suggestions? Anything I'm doing wrong?

  • ashlega Profile Picture
    34,477 on at

    Hi,

     #3 is displayed first since it's an async request, so you don't really get to #2 till there is a response from the server.

     Not sure you you are using 'initiatives' here:

    SDK.REST.retrieveRecord

     (

       parentId,

       'initiatives',

     Pretty sure it should be more like 'new_Initiative' (as Nithya suggested above)

     Also, here:

    function (initiatives)

       {

         testerVariable = initiatives.estcompletiondate;

     It should likely be initiatives.new_estcompletiondate;

     But it's case-sensitive.. You might want to use F12 to debug your code and see what attributes are available and where

  • Community Member Profile Picture
    on at

    8666.2017_2D00_07_2D00_11_5F00_14_2D00_11_2D00_58.jpg

    I used "new_Initiative" but got this error. I changed it to try to fix this problem.

    With the changes I just now made to the code, it now prints the error message every time I load my CRM page.

  • ashlega Profile Picture
    34,477 on at

    Might be lower-case then.. Try opening to that url without entity name:

    .../XRMServices/2011/OrganizationData.svc/

    Then search the "initiative". You should be able to find it there. If you find new_initiativeSet, then you need to use new_initiative. 

  • Community Member Profile Picture
    on at

    Got it! I'm not getting that error anymore. The code still isn't working holistically though. See the comments in my code below:

    function Form_Onload()
    {
      var parentId = Xrm.Page.getAttribute('new_initiativestepid').getValue()[0].id;
      alert(parentId + " 1"); // prints correctly
      var testerVariable = null;
        
      // utilize retrieveRecord method from REST library to get data based on parent id
      SDK.REST.retrieveRecord
      (
        parentId,
        'new_initiatives',
        null, null,
        function (new_Initiatives)
        {
          testerVariable = new_Initiatives.new_estcompletiondate;
          alert(testerVariable + " 2"); // prints "undefined"
        },
    
        function Error()
        {
          alert('There is error in reading initiative data');
        }
      );
    }

    Any ideas on how to solve the problem of retrieving the actual data value?

  • Verified answer
    ashlega Profile Picture
    34,477 on at

    It's probably another case of spelling the attribute correctly:) Try using debugger..

    Add

    debugger;

    Right before the second alert. Publish,refresh(or reopen the browser.. just in case). Then use f12 to open dev tools and trigger that script. Once it reaches the debugger line, it will pause, and you will be able to inspect the contents of your variable.

    Keep in mind you won't have an attribute there if it's null.

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