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 :
Microsoft Dynamics CRM (Archived)

Javascript to get the lookup id?

(0) ShareShare
ReportReport
Posted on by

Hi All,

          I have one dropdown with multiple items. I want to take the guid of the selected item in lookup on its chnged event. 

My code is like below, but Xrm.Page.data.entity.attributes.get('new_item'); this gives null . Anyone can figure it out?

var lookupItem = new Array();
lookupItem =Xrm.Page.data.entity.attributes.get('new_item');// Xrm.Page.getAttribute("new_item").getValue

if(lookupItem[0] != null)
{
var name = lookupItem[0].name;
var guid = lookupItem[0].id;
var entType = lookupItem[0].entityType;
}

*This post is locked for comments

I have the same question (0)
  • Guido Preite Profile Picture
    54,086 Moderator on at

    can you post a screenshot of this dropdown with multiple values?

  • Verified answer
    Bas van de Sande Profile Picture
    4,383 on at

    Hi,

    Arun Putti describes this on his blog:

    arunpotti.wordpress.com/.../get-lookup-id-text-entityname-in-crm-using-javascript

    I hope this helps you forward

    Bas

  • Community Member Profile Picture
    on at

    item.png

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi,

    Can you try this;

    Xrm.Page.getAttribute("lookupFieldName").getValue()[0].id;

    Regards,

    Please verify if it helps.

  • Verified answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    the right code will be

    var lookupItem = Xrm.Page.getAttribute("new_item").getValue();
    if (lookupItem != null)
    {
        var name = lookupItem[0].name;
        var guid = lookupItem[0].id;
        var entType = lookupItem[0].entityType;


    please note:

    the () after getValue

    the != null condition is checked against all the variable, not the first element (otherwise you get an error on null

    the } for the if condition, if you close the if after you set the variable, the guid variable will not be available after, you need to close the if later

  • Verified answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi Nidhin,

    Please try the following code.

    var lookupObj = Xrm.Page.getAttribute(lookupSchemaName); //Check for Lookup Object
    if (lookupObj != null) {
    var lookupObjValue = lookupObj.getValue();//Check for Lookup Value
    if (lookupObjValue != null) {
    var lookupEntityType = lookupObjValue[0].entityType, //To get EntityName
    lookupRecordGuid = lookupObjValue[0].id, // To get record GUID
    lookupRecordName = lookupObjValue[0].name; //To get record Name 
    }
    }
  • Community Member Profile Picture
    on at

    Its done. Thanks to all

  • Saddamk206 Profile Picture
    777 on at

    Hi,

    try these:-

    var Id = Xrm.Page.data.entity.attributes.get("fieldname").getValue()[0].id;

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

     

                             or Can Use if not work(Use window.parent. before Xrm)

    var Id = window.parent.Xrm.Page.data.entity.attributes.get("fieldname").getValue()[0].id;

    var rName = window.parent.Xrm.Page.data.entity.attributes.get("fieldname").getValue()[0].name;

  • Community Member Profile Picture
    on at

    var name = lookupItem[0].name; 
    var guid = lookupItem[0].id; 
    var entType = lookupItem[0].entityType; 

    why we use [0] ?

  • Suggested answer
    Bas van de Sande Profile Picture
    4,383 on at

    because it is an array of values.

    In order to get the id, you need to address the first element in the array, thus [0]

    I hope this helps you a bit further

    Bas

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans