Skip to main content

Notifications

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. 

  • naresh babu Profile Picture
    425 on at
    RE: Get lookup fields by using Rest builder

    Thanks Clofly.

  • Verified answer
    cloflyMao Profile Picture
    25,208 on at
    RE: Get lookup fields by using Rest builder

    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

  • elmaslouhi1 Profile Picture
    187 on at
    RE: Get lookup fields by using Rest builder

    Hi partner,

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

    Thank you

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

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 106 Most Valuable Professional

#3
Eugen Podkorytov Profile Picture

Eugen Podkorytov 102

Overall leaderboard

Product updates

Dynamics 365 release plans