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

I have the same question (0)
  • 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

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 74 Super User 2025 Season 2

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 31 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans