Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

How can I get the lookup value when I have its ID?

Posted on by 225

I've added a IFrame to a parent entity form. It gets all the parents children because the child knows its parent id. But the child has a lookup field, msdyn_tasktype.

In the list of children I don't get the field straight out... its actually "hiding" at  _msdyn_tasktype_value

1) Why is this?

2) How can I get the value of the tasktype, the _msdyn_tasktype_value contains the GUID?

If I was on a page of the child I would get it by; Xrm.Page.getAttribute('<FIELD_NAME>').getValue()[0]. But I'm on the parent page listing all the children and I would like to output the tasktype as it is a string value.

 

 

*This post is locked for comments

  • Suggested answer
    Mduduzi Profile Picture
    Mduduzi on at
    RE: How can I get the lookup value when I have its ID?

    Thanks Aric, that did the trick.

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: How can I get the lookup value when I have its ID?

    Hi,

    Are you getting the values using webapi?
    Field names that are specified as _fieldname_value have only a Guid.

    If you are using webapi, you will need to add the following to your function call in order to get the formatted values in option sets and the name/logical name in lookup controls:

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

    Example:
    var accountId = result["_accountid_value"];
    var accountName = result["_accountid_value@OData.Community.Display.V1.FormattedValue"];
    var accountLogicalName = result["_accountid_value@Microsoft.Dynamics.CRM.lookuplogicalname"];

    Hope this helps.
     

  • Suggested answer
    RE: How can I get the lookup value when I have its ID?

    Hi,

    Use this function to get the lookup data by passing each child record,

    ///<summary>

            /// Gets the lookup attribute value

            ///</summary>

            getLookupAttributeValue = function (result, attributeName) {

                if (result !== undefined && result !== null) {

                    if (result["_" + attributeName + "_value"] !== undefined && result["_" + attributeName + "_value"] !== null) {

                        return {

                            Id: result["_" + attributeName + "_value"],

                            LogicalName: (result["_" + attributeName + "_value@Microsoft.Dynamics.CRM.lookuplogicalname"] !== undefined && result["_" + attributeName + "_value@Microsoft.Dynamics.CRM.lookuplogicalname"] !== null) ? result["_" + attributeName + "_value@Microsoft.Dynamics.CRM.lookuplogicalname"] : null,

                            Name: (result["_" + attributeName + "_value@OData.Community.Display.V1.FormattedValue"] !== undefined && result["_" + attributeName + "_value@OData.Community.Display.V1.FormattedValue"] !== null) ? result["_" + attributeName + "_value@OData.Community.Display.V1.FormattedValue"] : null

                        };

                    }

                }

     

                return null;

            }

    Assume you are using Dynamics 365 V8.2 else you will not get LogicaName property from results.

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

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