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 :
Customer experience | Sales, Customer Insights,...
Answered

Get lookup fields by using Rest builder

(0) ShareShare
ReportReport
Posted on by 425

Hi experts,

I want to retrieve lookup fields(multiple records) data and set in single line of text field by using rest builder.

Thanks in advacne. 

I have the same question (0)
  • elmaslouhi1 Profile Picture
    187 on at

    Hi partner,

    to assist you, show us what you have tried to do to better understand the matter.

    Thank you

  • Verified answer
    cloflyMao Profile Picture
    25,210 on at

    Hi NB,

    As per my understanding, retrieving lookup fields data could be regarded as retrieving records of a specific entity,

    so we could run code below to achieve your requirement:(initialize a variable, then append retrieved name value to that variable in for loop)

    // var entity_type = formContext.getAttribute("lookupfield_name").getValue()[0].entityType;
    
    var list = "";
    
    var req = new XMLHttpRequest();
    req.open("GET", formContext.context.getClientUrl()   "/api/data/v9.1/accounts?$select=name", 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);
                for (var i = 0; i < result.value.length; i  ) {
                    if (i < result.value.length - 1) {
                        list  = result.value[i]["name"]   ", ";
                    } else {
                        list  = result.value[i]["name"];
                    }
                }
                formContext.getAttribute("new_introduction").setValue(list);
                console.log(list);
            } else {
                var result = JSON.parse(this.response);
                var alertStrings = { confirmButtonLabel: "Ok", text: result.error.message, title: "Error" };
                var alertOptions = { width: 200, height: 150 };
                Xrm.Navigation.openAlertDialog(alertStrings, alertOptions).then(
                    function success(result) {
                        console.log("Alert dialog closed");
                    },
                    function (error) {
                        console.log(error.message);
                    }
                );
            }
        }
    };
    req.send();

    e.g: Populate a custom single line of text field with all retrieved account records.

    pastedimage1590993716111v2.png

    Regards,

    Clofly

  • naresh babu Profile Picture
    425 on at

    Thanks Clofly.

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 72 Super User 2025 Season 2

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 29 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans