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,...
Suggested Answer

XMLHttpRequest leads to "Bad Request"

(0) ShareShare
ReportReport
Posted on by 45

Hello,

I am currently trying the following:


If an account is selected in a look-up field of the opportunity, another field should be filled with the content of a field on the respective account form.
The system used is version "1612 (8.2.15.2) (DB 8.2.15.2) local", so WebAPI cannot be used for this.
So far I have the following code:

function OnLoad(context) {
let formContext = context.getFormContext();
let parentaccount = formContext.getAttribute("parentaccountid").getValue();

if (parentaccount == null) {
return;
}

const type = parentaccount[0].entityType;

var req = new XMLHttpRequest();

req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/accounts" + "(" + parentaccount[0].id + ")" + "?$select=eno_vertreter1", 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 () {

alert("state: " + this.readyState + " status: " + this.status);

if (this.readyState === 4) {

req.onreadystatechange = null;

if (this.status === 200) {
var result = JSON.parse(this.response);

var representative = result["eno_accountvertreterid"];
alert("the representative: " + respresentative);
formContext.getAttribute("eno_accountvertreterid").value = representative;


} else {
alert("else");
Xrm.Utility.alertDialog(this.statusText);

}

}

};

req.send();


}


When running it, I get the following alerts:


"state: 2 status: 400"
"state: 3 status: 400"
"state: 4 status: 400"
"Bad Request"

Nothing else happens after that.
I have checked the names of the fields several times, there is certainly no error. I have used a similar logic on another system without any problems.

I am very grateful for any advice.

I have the same question (0)
  • Guido Preite Profile Picture
    54,086 Moderator on at

    are you sure your version is 8.2.15.2?

    because formContext was introduced with Dynamics version 9.0, so you have two ways:

    1) you are on 8.2 so the first part in order to get the value is wrong and you should use Xrm.Page in order to get the value

    2) you are on 9.0 or after and you can use Xrm.WebApi

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    HI,

    Based on your code I see you are selecting a field $select=eno_vertreter1 but you are trying to get completely different field

    var representative = result["eno_accountvertreterid"];

    Is this correct? I don't think so.

    Also, you can use DRB tool to auto generate your code instead of writting by your self.

    www.crmanswers.net/.../new-tool-dataverse-rest-builder.html

    Also, to check the detailed error you should open Browser Developer Tool -> Selecft Network tab-> Perform your operation -> You should see detailed error.

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • davalkan Profile Picture
    45 on at

    Hi Guido,

    thanks a lot for your help!

    1) If I log into the environment and navigate to Info it says "-Microsoft Dynamics 365 -Version 1612 (8.2.15.2) (DB 8.2.15.2) local" in the window "About Dynamics 365). So yes I believe it is 8.2. However, the id of the accountparent-field is correct. I printed it using an alert statement, so I think that formContext seems to work?

    2) I first tried this using Xrm.WebApi but it resulted in Errors. I then read that it is not available for systems <9.0 so thats the reason why I did not use it.

  • davalkan Profile Picture
    45 on at

    Hello Bipin, thanks for your help!

    I think you are right, in the status === 200 block it should be

    var representative = result["eno_vertreter1"];

    instead of

    var representative = result["eno_accountvertreterid"];

    However, my code does not even reach that point when executing it, since the status is 400, not 200.

  • Guido Preite Profile Picture
    54,086 Moderator on at

    I don't know why formContext is working on your instance to be honest but I checked the XMLHttpRequest code and is correct.

    As Bipin pointed out you are selecting a field (eno_vertreter1) and trying to access one inside the result (eno_accountvertreterid) 400 means that the request is not well-formed, so probably the field eno_vertreter1 does not exists

    hope it helps

  • Guido Preite Profile Picture
    54,086 Moderator on at

    and last thing, if the field is a lookup it should be with the sintax _ABC_value so

    eno_vertreter1

    becomes

    _eno_vertreter1_value

  • davalkan Profile Picture
    45 on at

    Thank you very much.

    I tested if there is any difference if I use eno_vertreter1 instead of eno_accountvertreterid but I still get the same error.

    The logical name of the field on the account form that I am trying to read is eno_vertreter1.

    It might have been important to mention: the field that I want to read as well as the one that I am trying to write to are both Look up fields. However replacing eno_vertreter1 with _eno_vertreter1_value didnt solve the issue either.

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at

    Hi,

    Have you tried any of the approach I provided earlier?

    you can use DRB tool to auto generate your code instead of writting by your self.

    www.crmanswers.net/.../new-tool-dataverse-rest-builder.html

    Also, to check the detailed error you should open Browser Developer Tool -> Selecft Network tab-> Perform your operation -> You should see detailed error.

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

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 81 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

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

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans