How to get different field values in a lookup field other than primary field value form another entity.
How to get different field values in a lookup field other than primary field value form another entity.
Hi,
You can use the expand in your query to retrieve the lookup field value from another entity using Xrm.Webapi. An example is shown below :-
//getting of userid from the context
var userId = Xrm.Utility.getGlobalContext().userSettings.userId.replace("{", "").replace("}", "");
//fetching user by id and expanding roles to get role names
Xrm.WebApi.retrieveRecord("systemuser", userId, "?$expand=systemuserroles_association($select=name)").then(
function success(result) {
//looping through all the roles available for user
for (var i = 0; i < result.systemuserroles_association.length; i++) {
//getting current role name
var roleName = result.systemuserroles_association[i]["name"];
}
},
function (error) {
Xrm.Navigation.openAlertDialog({ text: error.message });
}
Please mark the answer as verified if it is useful to you !!
Hi,
Use expand properties in web api to get other field from lookup entity record.
GET [Organization URI]/api/data/v9.0/accounts(00000000-0000-0000-0000-000000000001)?$select=name&$expand=primarycontactid($select=contactid,fullname) HTTP/1.1
As you can see in above api call, We are getting contact full name from primary contact lookup on Account.
Hi Partner,
Lookup field only save three properties: record id, record name and record entity name, it is by design.
You could create an extra query request to get more fields of the selected record.
The code below is JavaScript version.
var entityId = Xrm.Page.getAttribute("lookup_field_name").getValue()[0].id.replace('{', '').replace('}', '').toLowerCase(); var entityName = Xrm.Page.getAttribute("lookup_field_name").getValue()[0].entityType; var req = new XMLHttpRequest(); req.open("GET", Xrm.Page.context.getClientUrl() "/api/data/v8.2/" entityName "s" "(" entityId ")", true); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.setRequestHeader("Content-Type", "application/json; charset=utf-8"); req.onreadystatechange = function () { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 200) { var result = JSON.parse(this.response); console.log(result); } else { alert(this.response); } } }; req.send();
Please take articles below as reference for lookup field description:
Regards,
Clofly
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,280 Super User 2024 Season 2
Martin Dráb 230,235 Most Valuable Professional
nmaenpaa 101,156