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)

get lookup field value through javascript

(0) ShareShare
ReportReport
Posted on by 2,510

Hi, I have requirement to get a customer name from lookup field, it looks like " xrm.page.getattributes"  doesn't in this way. please help

*This post is locked for comments

I have the same question (0)
  • Verified answer
    ashlega Profile Picture
    34,477 on at

    You can try

    var name = Xrm.Page.getAttribute('<FIELD_NAME>').getValue()[0].name;

    (you can also use "id":  Xrm.Page.getAttribute('<FIELD_NAME>').getValue()[0].id;)

    EDIT: Of course I forgot to put getValue there - updated the examples.. Thanks Deepthi Thotakura 

  • Verified answer
    Community Member Profile Picture
    on at

    Hi ,

    If you are using to get lookup details through JavaScript , Here is example :

    if (Xrm.Page.data.entity.attributes.get("fieldname").getValue() != null) {

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

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

               var CustomerType = Xrm.Page.data.entity.attributes.get("feildname ").getValue()[0].entityType;

           }

    //Display the lookup name

    alert(CustomerName);

    I hope this helps you  replace fieldname with your lookup field name in Form 

    let me know if you find any errors.

    Thanks ,

    Deepthi

  • Jonathan Davila Profile Picture
    on at

    This is the best answer and actually details all the data that you can get from lookup value.

  • Suggested answer
    LMP Profile Picture
    680 on at

    Additional info.

    You have to check first if the field is not null before getting its value otherwise you will get a JavaScript error.

    if (Xrm.Page.getAttribute(fieldName) != null)

    var id = Xrm.Page.getAttribute(fieldName).getValue()[0].id; // If you would like to get the id of the record on the lookup field.

  • Community Member Profile Picture
    on at

    thank you it works

  • Community Member Profile Picture
    on at

    Hello , I currently need help and have multiple questions about this.

    What is the [0] for?

    Let's say I want to get something else than the name, can i use "getValue()[0].customfield" to get the custom field in question?

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hello,

    This is you need when you are getting value frof lookup field which contains I'd and name .for that you need to write like below

    getValue()[0].id

    getValue()[0].name

    Hope this helps

  • 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;

  • KashyapT Profile Picture
    106 on at

    What about other field values of the lookup record ?

  • Jharana Baliyar Singh Profile Picture
    2,667 on at

    Please create a new thread about your queries as this thread got verified already.

    Thanks,

    Jharana

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