Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Get lookup value from XMLHTTPRequest

Posted on by 115

I created js function to get record from different entity in my organization. 

function retrieveRecord() {
	var recordId = "62066436-52de-eb11-b802-00155d6e5e00";
    //var clientURL = Xrm.Page.context.getClientUrl();
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl()   "/api/data/v8.2/lnkt_actiontakens(" recordId  ")?$select=lnkt_name,lnkt_Device,lnkt_SubDevice,lnkt_Damage,lnkt_RootCause", true);

    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json;charset=utf-8");
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.onreadystatechange = function () {
        if (this.readyState == 4) {
            req.onreadystatechange = null;
            if (this.status == 200) {
                var data = JSON.parse(this.response);     
				var action_name = data.lnkt_name;
				var device = data.lnkt_device;
				if(device != null)
				{
					var deviceId = device[0].id;
				}
				alert("Action name: "   action_name   " ,Device ID: "   deviceId);
            }
            else {
                var error = JSON.parse(this.response).error;
                alert("Error retrieving Record – "   error.message);
            }
        }
    };
    req.send();
}

I can get primary field which is "lnkt_name" (Single Line of Text). However i can't get the value of look up (lnkt_device). In the alert its return undefinied. How to get the  lookup id, lookup entityname, and lookup label. Thanks

  • FikriHailal Profile Picture
    FikriHailal 115 on at
    RE: Get lookup value from XMLHTTPRequest

    Thanks. I used crm rest builder and its work

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Get lookup value from XMLHTTPRequest

    Hi Partner,

    Has the problem been solved? Any updates?

    Please click Yes under "Did this answer your question?" to close this thread.

     3757.pastedimage1625825335014v1.png

    Thanks. 

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

  • Suggested answer
    Vipin J Profile Picture
    Vipin J 1,583 on at
    RE: Get lookup value from XMLHTTPRequest

    Please ensure if you have value in respective field.

    Refer here : https://vjcity.blogspot.com/2019/07/how-to-get-and-set-value-for-different.html

    https://vjcity.blogspot.com/2019/12/most-common-javascript-methods-for.html

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Get lookup value from XMLHTTPRequest

    Hi FikriHailal,

    You can build request easily with the tool—CRM REST Builder:

    Download page: https://github.com/jlattimer/CRMRESTBuilder

    Download it and import it to solution, then one icon will appear like following screenshot:

    3833.pastedimage1625641107271v6.png

    For example, Contact has one lookup field(Account name):

    5280.pastedimage1625640694978v1.png

    Settings in CRM REST Builder:

    2728.pastedimage1625640772786v3.png

    Clicking Create request:

    0458.pastedimage1625640906416v4.png

    Execute code:

    2450.pastedimage1625640958891v5.png

    Regards,

    Leah Ju

    Please mark as verified if the answer is helpful. Welcome to join hot discussions in Dynamics 365 Forums.

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans