I am trying to get a lookup value from a related entiy and I get a "Bad Request" error message.
On the form where I load this script, the lookup field is called "new_projet". It's a lookup to an entity that is also called "new_projet".
From the new_projet entity, I want to get the value from the field named "new_typedactivite".
The "....URL/api/data/v8.2/new_projets" URL renders correctly so I know I am pointing at the right place. It renders a list of Projects. One example:
"@odata.etag":"W/\"183037263\"","versionnumber":183037263,"_owningbusinessunit_value":"c9a2ef4c-8c8c-e011-af9b-005056846389","_new_typedactivite_value":"83b47c04-ac4e-ea11-8151-0050569871d9","statecode":0,"statuscode":1,"_createdby_value":"6813b03a-49c3-e011-ba26-005056846389","new_titre":"Projet #3","_ownerid_value":"6813b03a-49c3-e011-ba26-005056846389","new_numero":"000001","modifiedon":"2020-02-26T04:25:42Z","new_projetid":"48ee9b1b-ac4e-ea11-8151-0050569871d9","_owninguser_value":"6813b03a-49c3-e011-ba26-005056846389","_modifiedby_value":"6813b03a-49c3-e011-ba26-005056846389","_new_compte_value":"cb423991-b923-e811-80f5-005056b929fd","new_typedeprojet":100000001,"createdon":"2020-02-13T21:59:30Z","_createdonbehalfby_value":null,"overriddencreatedon":null,"importsequencenumber":null,"timezoneruleversionnumber":null,"_owningteam_value":null,"utcconversiontimezonecode":null,"_modifiedonbehalfby_value":null
But when I run the below code onload of the form, I get a "Bad Request" error". I'm pretty sure there is only one little thing missing... So any help would be much appreciated:
function gettypeactivite() {
//Make sure Projet field not null
var projet = Xrm.Page.getAttribute("new_projet").getValue();
//If not null, perform any basic checking to make sure we have a valid starting value
if (projet != null) {
//Define field to request
var projetId = projet[0].id;
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_projets(" + projetId + ")?$select=_new_typedactivite_value", 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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function() {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(this.response);
alert(result);
var value = result["_new_typedactivite_value"];
var formatted = result["_new_typedactivite_value@OData.Community.Display.V1.FormattedValue"];
var lookuplogicalname = result["_new_typedactivite_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
}
}