Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

Getting Lookup field Value

Posted on by Microsoft Employee

How to get different field values in a lookup field other than primary field value form another entity.

  • Suggested answer
    Aditya Profile Picture
    Aditya 260 on at
    RE: Getting Lookup field Value

    Hi,

    You can use the expand in your query to retrieve the lookup field value from another entity using Xrm.Webapi. An example is shown below :-

    //getting of userid from the context
    var userId = Xrm.Utility.getGlobalContext().userSettings.userId.replace("{", "").replace("}", "");

    //fetching user by id and expanding roles to get role names
    Xrm.WebApi.retrieveRecord("systemuser", userId, "?$expand=systemuserroles_association($select=name)").then(
    function success(result) {

    //looping through all the roles available for user
    for (var i = 0; i < result.systemuserroles_association.length; i++) {
    //getting current role name
    var roleName = result.systemuserroles_association[i]["name"];
    }


    },
    function (error) {
    Xrm.Navigation.openAlertDialog({ text: error.message });
    }

    Please mark the answer as verified if it is useful to you !!

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Getting Lookup field Value

    Hi,

    Use expand properties in web api to get other field from lookup entity record.

    GET [Organization URI]/api/data/v9.0/accounts(00000000-0000-0000-0000-000000000001)?$select=name&$expand=primarycontactid($select=contactid,fullname) HTTP/1.1  

    As you can see in above api call, We are getting contact full name from primary contact lookup on Account.

    If found helpful, Please mark my answer verified.

  • Suggested answer
    cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Getting Lookup field Value

    Hi Partner,

    Lookup field only save three properties: record id, record name and record entity name, it is by design.

    You could create an extra query request to get more fields of the selected record.

    The code below is JavaScript version.

    var entityId = Xrm.Page.getAttribute("lookup_field_name").getValue()[0].id.replace('{', '').replace('}', '').toLowerCase();
    
    var entityName = Xrm.Page.getAttribute("lookup_field_name").getValue()[0].entityType;
    
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl()   "/api/data/v8.2/"   entityName   "s"   "("   entityId   ")", 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.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var result = JSON.parse(this.response);
                console.log(result);
            } else {
                alert(this.response);
            }
        }
    };
    req.send();

    Please take articles below as reference for lookup field description:

    https://docs.microsoft.com/en-us/previous-versions/dynamicscrm-2016/developers-guide/gg334767(v=crm.8)#retrieve-data-about-lookup-properties

    https://docs.microsoft.com/en-us/previous-versions/dynamics-crm4/developers-guide/cc189833(v=msdn.10)

    Regards,

    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

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans