Hi All,
My javascript skills is still shaky,I want to know how to get values from a lookup field entity using javascript. After retrieving the lookup field on the form, like the below;
var name = Xrm.Page.getAttribute('<FIELD_NAME>').getValue()[0].name;
How do i go one step deeper to retrieve values of fields from the retrieved lookup field?
Thank for the help in advance.
*This post is locked for comments
Thanks Andrew once again.
Thanks Ravi. I will look into the tool, as i have been having problems even debugging javascript.
Hi Victor, You can get your solution by using below code
if (Xrm.Page.getAttribute("<FIELD_NAME>).getValue() != null) {
var LookupArray = new Array();
LookupArray = Xrm.Page.getAttribute("<FIELD_NAME>").getValue();
if (LookupArray != null) {
var LookUpName = LookupArray [0].name;
var LookUpId = LookupArray [0].id;
var LookUpentityType = LookupArray [0].entityType;
}
}
Hope this will help.
Hi Victor,
You can refer the below link on retrieving the lookup values.
As you have started with JavaScript, I would also like to introduce you to the Rest Builder tool (if you are not already aware). This is the easiest way to build web api query as well as execute it and see the results.
Refer this blog for details: carldesouza.com/dynamics-crm-rest-builder
You can download the tool here- github.com/.../releases
Hope this helps.
If you want to retrieve data from lookup entity
Get record id and entity type from lookup then perform a web API call to retrieve required fields.
Please follow the links
docs.microsoft.com/.../query-data-web-api
Hi Victor,
From a lookup field, you can get the Name, Id and EntityType of the record. Below screenshot shows how to get them.
If you are using Dynamics 365 version 9.0+, you should start using formContext and globalContext instead of Xrm.Page since Xrm.Page will be deprecated soon. Check the below link for more information regarding this.
Hello,
To get values you will have to use Crm Endpoints. Here are examples:
docs.microsoft.com/.../mt770365(v%3dcrm.8)
If you're 9.xthen you can use Xrm.WebApi - butenko.pro/.../microsoft-dynamics-365-v9-0-usage-of-new-oob-webapi-functions-part-1
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156