Create related entity records along with the primary record in D365 using Web API Javascript - XMLHttpRequest
You have Person and Person Address custom entity. you wanted to call web API to create records for person and person address. You have added required web resources in CRM.
var Gender;
var Indian;
var Technologies = "";
var firstName;
var lastName;
function CreatePerson() {
firstName = document.getElementById("firstName").value;
lastName = document.getElementById("lastName").value;
var data =
{
"new_name": firstName + " " + lastName,
"new_firstname": document.getElementById("firstName").value,
"new_lastname": document.getElementById("lastName").value,
"new_dob": document.getElementById("DOB").value,
"new_age": document.getElementById("Age").value,
"new_gender": Gender,
"new_indian": Indian,
"new_salary": parseInt(document.getElementById("Salary").value),
"new_technology": Technologies,
"regardingobjectid_new_personaddress":
[{
"new_address": document.getElementById("Address").value
}
]
}
debugger;
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/new_persons", false);
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 () {
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 {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(JSON.stringify(data));
}
function CreatePersonAddress(personId) {
var data =
{
"regardingobjectid_new_personaddress": personId,
"new_address": document.getElementById("Address").value
}
debugger;
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/new_personaddresses", false);
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 () {
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 {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(JSON.stringify(data));
}
regardingobjectid_new_personaddress is navigation property.
var Gender;
var Indian;
var Technologies = "";
var firstName;
var lastName;
function CreatePerson() {
firstName = document.getElementById("firstName").value;
lastName = document.getElementById("lastName").value;
var data =
{
"new_name": firstName + " " + lastName,
"new_firstname": document.getElementById("firstName").value,
"new_lastname": document.getElementById("lastName").value,
"new_dob": document.getElementById("DOB").value,
"new_age": document.getElementById("Age").value,
"new_gender": Gender,
"new_indian": Indian,
"new_salary": parseInt(document.getElementById("Salary").value),
"new_technology": Technologies,
"regardingobjectid_new_personaddress":
[{
"new_address": document.getElementById("Address").value
}
]
}
debugger;
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/new_persons", false);
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 () {
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 {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(JSON.stringify(data));
}
function CreatePersonAddress(personId) {
var data =
{
"regardingobjectid_new_personaddress": personId,
"new_address": document.getElementById("Address").value
}
debugger;
var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/new_personaddresses", false);
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 () {
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 {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send(JSON.stringify(data));
}
regardingobjectid_new_personaddress is navigation property.
Comments
Post a Comment