RE: newEntityId (Id from new record created) returning null upon new record creation
Hello,
I a not sure which version of rest builder are you using I would recommend download the rest builder from below -
https://github.com/jlattimer/CRMRESTBuilder/releases
and import the managed solution in your CRM instance.
Here is the step I have followed.
Step1 : Select the attributes to pass while creating record.
Step 2 : Click on Create Request .
Step 3 Here is the id I am getting
Now here is my Code , where I have declare a variable in the first line for entity id and passing this to outside and inside of the method.
function ClickMe() {
//local variable for newEntityId
var CreatedEntityID = null;
//Created from rest builder
var entity = {};
entity.firstname = "Goutam";
entity.lastname = "Das";
entity.telephone1 = "8598989878";
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contacts", 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];
CreatedEntityID = newEntityId;//Assign the value to the local variable
// Pass to other method
CreateAnotherRecord(CreatedEntityID);
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(JSON.stringify(entity));
// Pass to other method
CreateAnotherRecord(CreatedEntityID);
}
Hope this helps.