This project is read-only.
3

Closed

Issue running create with lookup against CRM 2016

description

I keep getting a Bad Request error trying to create a record that contains a lookup. Have tried two different CRM 2016 on-prem systems, one with the latest rollup, one with an out of box install.

Each system has different entities

This is the generated code, which is very simple. It works fine with the 2011 endpoint, and is fine if I remove the lookups. Is there a known issue with lookups.

Thanks for any insights...

var entity = {};
entity.new_name = "vcvcvc";
entity.new_corpsid = {
        Id: "01F96C87-44FF-E511-80CE-00155D000202",
        LogicalName: "new_corps"
    };
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/new_assignments", 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) {
        var uri = this.getResponseHeader("OData-EntityId");
        var regExp = /\(([^)]+)\)/;
        var matches = regExp.exec(uri);
        var newEntityId = matches[1];
    }
    else {
        alert(this.statusText);
    }
}
};
req.send(JSON.stringify(entity));

This is code for 2011, which works fine..

var entity = {};
entity.new_name = "vcvcvc";
entity.new_corpsId = {
        Id: "01F96C87-44FF-E511-80CE-00155D000202",
        LogicalName: "new_corps"
    };
var req = new XMLHttpRequest();
req.open("POST", encodeURI(Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/new_assignmentSet"), true);
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function () {
if (this.readyState === 4) {
    this.onreadystatechange = null;
    if (this.status === 201) {
        var result = JSON.parse(this.responseText).d;
        var newEntityId = result.new_assignmentId;
    }
    else {
        alert(this.statusText);
    }
}
};
req.send(JSON.stringify(entity));
Closed Oct 19, 2016 at 1:40 AM by JLattimer

comments

jameshouck wrote Sep 16, 2016 at 7:07 PM

Hey Perkin - I believe I found the issue. I think the property types for lookups are not correctly formatted. You need to append "@odata.bind" to the property in the JSON string. Check out this link from MSDYN documentation for the oData 4 endpoint: here and look under the Associate an Entity section. When I passed in my own JSON formatted like this it worked.

However, I'm now trying to create a Quote Product (quotedetail) and I am having an issue trying to set the productid this way. It worked for the QuoteId lookup, but not the product. If you can think of anything, please let me know!

Thanks,
James

PerkinTheHermit wrote Sep 17, 2016 at 6:08 PM

Hi James,

Thanks for the update - will give that a try - thought it must be some form of minor formatting issue.

Andrew P

JLattimer wrote Oct 19, 2016 at 1:40 AM

Being fixed in the next release

wrote Oct 19, 2016 at 1:40 AM