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

  • yleclerc Profile Picture
    1,549 on at
    RE: Web API - Bad Request when trying to get Lookup Value

    Thanks very much H V!

    This is what I was missing!

  • Verified answer
    H V Profile Picture
    351 on at
    RE: Web API - Bad Request when trying to get Lookup Value

    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!

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 78 Super User 2025 Season 1

#3
Sahra Profile Picture

Sahra 43

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans