web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Web API - Bad Request when trying to get Lookup Value

(0) ShareShare
ReportReport
Posted on by 1,549

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();
}
}

  • Verified answer
    H V Profile Picture
    351 on at

    Hi Yvan,

    Please remove Curly brackets from projetId. When you are getting value from the lookup, it return lookup id with curly brackets. So you need to replace with blank.

    //Define field to request
    var projetId = projet[0].id;
    
    projetId = projetId.replace("{","");
    projetId = projetId.replace("}","");
    

    Hope this helps!

  • yleclerc Profile Picture
    1,549 on at

    Thanks very much H V!

    This is what I was missing!

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
11manish Profile Picture

11manish 109 Super User 2026 Season 2

#2
Syed Aqib Raza Profile Picture

Syed Aqib Raza 88

#3
ParthPatel249 Profile Picture

ParthPatel249 82

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans