Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Retrieve fields from related entity using Jscript

Posted on by Microsoft Employee

Good afternoon,

I'm looking for a way to retrieve fields from a related entity using Jscript.

I've had a look around the internet and it appears a lot of their solutions are prior to Dynamics 365 using OData feeds and unsupported plugins.

Does anyone have a script that works in Dynamics 365 Online?

I would prefer to steer clear of workflows as I want to retrieve the information instantly, rather than on save.

Thanks in advance!

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Retrieve fields from related entity using Jscript

    SDK.WEBAPI.retrieveRecord(

               contactValue[0].id,

               "contact",

               "jobtitle",

               "",

               function (result) {

                   console.log(result);

                   var jobtitle= result.JobTitle;

                  // Xrm.Page.getAttribute("jobtitle").setValue(jobTitle);

               },

               function (error) {

                    console.log(error);

               }

           );

    "Error : 400: : undefined", message: "Error : 400: : undefined", stack: "Error: Error : 400: : undefined at SDK.WEBAPI._errorHandler ()

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Retrieve fields from related entity using Jscript

    Tom ! Don't fail me on the last hurdle... we've come so far ! :)

    I'll try that approach... !

    Thanks again.

  • Thomas David Dayman Profile Picture
    Thomas David Dayman 11,323 on at
    RE: Retrieve fields from related entity using Jscript

    Hi CAdams,

    I had a look for you as I haven't done this either. I think this article might help:

    deepakexploring.wordpress.com/.../retrieve-option-set-text-value

    Although you create another thread and maybe you can get a more concrete answer from someone who is more familiar.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Retrieve fields from related entity using Jscript

    Sorry Tom.. one last question.

    How would you return the text value from a retrieved option set - it appears .setText isn't working ! All I get back is the value.

    Thanks in advance.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Retrieve fields from related entity using Jscript

    Tom, it worked ! You're amazing !

    I'll post the final code when I've finished.

    Thanks again for all your help, I really appreciate it !!

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Retrieve fields from related entity using Jscript

    Blank.

  • Verified answer
    Thomas David Dayman Profile Picture
    Thomas David Dayman 11,323 on at
    RE: Retrieve fields from related entity using Jscript

    Hi CAdams,

    I'm more familiar with the SDK.REST Format. This what the interface would look like for your instance.

    4532.Screenshot_5F00_1.png

    The code would look like this:

    var contact = Xrm.Page.getAttribute("header_process_parentcontactid").getValue();
    
    SDK.REST.retrieveRecord(
        contact[0].id,
        "Contact",
        "JobTitle",
        null,
        function (result) {
            var jobTitle = result.JobTitle;
            Xrm.Page.getAttribute("jobtitle").setValue(jobTitle);
        },
        function (error) {
             alert(error.message);
        }
    );


    You can put this code in onLoad, onChange or onSave.

    Let me know how you get on. Also dont forget to add the SDK WebAPI code I posted above on the form to work.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Retrieve fields from related entity using Jscript

    Hi Tom,

    I've been playing around with the CRM Rest Builder and am a little stuck (novice here !!).

    I'm trying to retrieve the job title of the contact within the leads entity (i.e. using parent contact id field).

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/leads(DCCE5294-9447-E711-8104-5065F38A4A21)?$expand=parentcontactid($select=jobtitle)", true);

    req.setRequestHeader("OData-MaxVersion", "4.0");

    req.setRequestHeader("OData-Version", "4.0");

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

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

    req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");

    req.onreadystatechange = function() {

       if (this.readyState === 4) {

           req.onreadystatechange = null;

           if (this.status === 200) {

               var result = JSON.parse(this.response);

               var leadid = result["leadid"];

               if (result.hasOwnProperty("parentcontactid")) {

                   var parentcontactid_jobtitle = result["parentcontactid"]["jobtitle"];

               }

           } else {

               Xrm.Utility.alertDialog(this.statusText);

           }

       }

    };

    req.send();

    It appears that the result will post a value when I save the record and not before...? I've tried changing true to false as well and it's making no difference.

    Thanks in advance,

  • Verified answer
    Thomas David Dayman Profile Picture
    Thomas David Dayman 11,323 on at
    RE: Retrieve fields from related entity using Jscript

    You will have to download this JavaScript file and add it as a Web Resource. You will need place it in any form you wish to use the WebAPI

    sdkwebapi.codeplex.com

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Retrieve fields from related entity using Jscript

    Thanks all for the replies !

    Thanks Tom, I'll try that now.

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,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans