Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Xrm Coding issue

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Hi All,

Please have a look on this simple code:

function getLookupDetails() {
     var res = Xrm.Page.getAttribute("new_mcdetailid").getValue()[0].Name;
     console.log(res);
}

this is running on onChange event of the lookup field named new_mcdetailid, and upon execution, I got 'res' variable come as 'undefined' but it should have 'name' property of the selected record of this lookup field. So where I am going wrong?

My task is to get selected value from this lookup and then fetch some values from another entity based on this selection, so If anybody can suggest full code (my task is to do it only using JavaScript), I shall be grateful.

Thanks in Advance

*This post is locked for comments

  • Verified answer
    Prashant Maurya Profile Picture
    Prashant Maurya 236 on at
    RE: Xrm Coding issue

    Hi Imran

    you should use "[0].name" instead of "[0].Name"

    here is an example for different CRM version

    CRM V8.2 and older version

    var lookup = Xrm.Page.getAttribute("lookfieldname");

    if(lookup != null)

    {

    val value = lookup.getValue()[0].name

    }

    CRM V9+

    var formContext = executionContext.getFormContext();

    var lookup = formContext.getAttribute("lookfieldname");

    if(lookup != null)

    {

    val value = lookup.getValue()[0].name

    }

  • Verified answer
    gdas Profile Picture
    gdas 50,089 on at
    RE: Xrm Coding issue

    Hi Imran,

    "Name" property should be in lower case (name) . Here is the correct code . In addition it is always good practice always having null check to get the value from control.

    Secondly make sure your attribute name is correct.

    function getLookupDetails() {
    if (Xrm.Page.getAttribute("new_mcdetailid") != null && Xrm.Page.getAttribute("new_mcdetailid") != undefined) {
    var res = Xrm.Page.getAttribute("new_mcdetailid").getValue()[0].name;
    console.log(res);
    }
    }

  • Suggested answer
    Aric Levin Profile Picture
    Aric Levin 30,188 on at
    RE: Xrm Coding issue

    Hi Imran,

    A couple of things.

    First, I would check that Xrm.Page.getAttribute("new_mcdetailid") does not return null or undefined

    if the result is correct then I would get the value with the name attribute (name has to be lowercase).

    Should be something like this:

    function getLookupName(fieldName) {

       var field = Xrm.Page.getAttribute(fieldName);

       if (field != null) {

           var fieldId = field.getValue();

           if (fieldId != null)

               return fieldId[0].name.toString();

           else

               return '';

       }

    }

    Hope this helps.

  • Verified answer
    EnriqueMdz Profile Picture
    EnriqueMdz 1,065 on at
    RE: Xrm Coding issue

    Hi Imran, hope you're doing good.

    The correct way to get the Name attribute of a Lookup is:

    var LookupName = Xrm.Page.data.entity.attributes.get("FIELD_NAME").getValue()[0].name;

    Tha difference between your code line and mine is that .name is lower case.

    Please mark this answer as verified if helpfull.

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 Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,489 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans