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 :
Microsoft Dynamics CRM (Archived)

Web API PATCH 400 Bad Request - Javascript

(0) ShareShare
ReportReport
Posted on by 142

Hi

I am getting a bad request when using the below code to send an update to a custom entity. The field is a lookup to the same entity as a reference to another instance of the entity. I have obfuscated some of the field names to protect sensitive data. Anyone have any ideas what's going on?

var target = guidoftarget;

var entity = {};
entity["mergedto@odata.bind"] = "/new_salescontracts(" + target + ")";

var req = new XMLHttpRequest();

req.open("PATCH", encodeURI(Xrm.Page.context.getClientUrl() + "/api/data/v8.1/new_salescontracts(" + guidofrecordtobeupdated + ")"), true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 204 || this.status == 1223) {
 console.log("Success");
}
else {
alert(this.statusText);
}
}
};
req.send(JSON.stringify(entity));

*This post is locked for comments

I have the same question (0)
  • Rawish Kumar Profile Picture
    13,758 on at

    it should simply be like : 

    req.open("PATCH", Xrm.Page.context.getClientUrl() + "/api/data/v8.1/new_documents("+newid+")", true);

  • Community Member Profile Picture
    on at

    Assuming that "mergedto" attribute is correct and the entity name is "new_salescontract" I would check if you have stripped the { } from the "target" and "guidofrecordtobeupdated" guids.

  • Community Member Profile Picture
    on at

    Hi viperdudeuk,

    i dont see anywhere code to clean guid.

    Remember always to do it:

    var finalguid = yourguid.replace('{', '').replace('}', '');

    Please let me know if you solve.

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • viperdudeuk Profile Picture
    142 on at

    Hi

    In the console the guid in the URL appears like this

    new_salescontracts(5e3309bf-3ef5-e711-80da-0050569b5fd4)

    and the guid for the mergeto attribute as fabca294-0ef6-e711-80da-0050569b5fd4

    both without {}

  • viperdudeuk Profile Picture
    142 on at

    The {} were cleaned in another function and then passed into the function that raises the error.

  • viperdudeuk Profile Picture
    142 on at

    Tried that, same error.

  • viperdudeuk Profile Picture
    142 on at

    Some more information.

    If I do a GET on the URL that reports as Bad Request on the PATCH request it loads the entity record as ODATA in the browser.

    The JSON that is being sent in the PATCH looks like this.

    {"mergedto@odata.bind":"/new_salescontracts(eae73e6c-12f6-e711-80da-0050569b5fd4)"}

  • Suggested answer
    Justinjose Profile Picture
    2,707 on at

    Hi Jon Farmer,

    As you said you are trying to update an entity lookup field with in the custom entity, are you missing the prefix of this field "mergedto@odata.bind" ? if you dont mind can you please provide the exact name of the look field you are trying to update?

    or can you please try changing the green marked name ("mergedto@odata.bind") with the entity lookup schema name.

    Thanks

  • viperdudeuk Profile Picture
    142 on at

    Hi

    I am deleting the prefix from what I post on here as it would reveal client information. However I am using the prefix going as far as copying and pasting exactly from the field configuration page.

  • Suggested answer
    Justinjose Profile Picture
    2,707 on at

    Hi,

    Can you try schame name please. for example: entity["new_Fieldname@odata.bind"]. Normally after the prefix the first letter should be capital.

    Thanks

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans