Hi,
I have come across an error while using web API, I was trying to retrieve and update values from one of entities.
Below is the API request.
var id = Xrm.Page.data.entity.getId();
var entity = {};
entity.statecode = 1;
entity.statuscode = 2;var req = new XMLHttpRequest();
req.open(“PATCH”, Xrm.Page.context.getClientUrl() + “/api/data/v8.0/quotes(” + id + “)”, 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.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 204) {
//Success – No Return Data – Do Something
}
else {
alert(this.statusText);
}
}
};
req.send(JSON.stringify(entity));
When i execute it returned me saying
Then i have looked into some articles about Web API in web , which did not helped me.
After spending some time, I realized that when i take current entity id it comes with {}(flower brackets) and the same is being passed to API which in turn becomes a bad request.
Then i have replace {} with space then it worked like charm.
id = id.replace(/\{|\}/gi, ”);
This looks like a small issue but i am damn sure it will waste your time.
Hope it helps.
Thank you,
Sreeni Pavalla
Hi Sreeni, thanx for your post, but the resolution you posted is missing singe quotes:
id = id.replace(/\{|\}/gi, ”);
should be
id = id.replace(‘/\{|\}/gi, ”’);
LikeLiked by 1 person
yeah it was typo… will update it. Thanks Kalix
LikeLike
I faced same error and used slice method ( ID = ID.slice(1, -1); )
LikeLiked by 1 person
thx bro !
LikeLiked by 1 person
Hi,
id = id.replace(‘/\{|\}/gi, ”’); this is also not working through syntax error.
LikeLiked by 1 person