For this example, we'll create a Contact record, when clicking a button, using a Web Resource locally inside the Form.
How to Create a new Entity record using Web API and Javascript in 5 minutes
This is the function to Create a Record , using an HTTP POST request:
var entity = {};
entity.address1_telephone1 = "";
entity.emailaddress1 = "";
entity.lastname = "";
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contacts",
data: JSON.stringify(entity),
beforeSend: function(XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");
XMLHttpRequest.setRequestHeader("OData-Version", "4.0");
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
async: true,
success: function(data, textStatus, xhr) {
var uri = xhr.getResponseHeader("OData-EntityId");
var regExp = /\(([^)]+)\)/;
var matches = regExp.exec(uri);
var newEntityId = matches[1];
},
error: function(xhr, textStatus, errorThrown) {
Xrm.Utility.alertDialog(textStatus + " " + errorThrown);
}
});
And this is the Web Resource EMBEDDED in the Form (that's why we wrote "parent" before calls to JQuery ) :
1) Open the Form
2) Add > Section > Web Resource
3) Paste inside it the following HTML - JS code:
<html><head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script>
var fnCreateContact = () => {
var entity = {};
entity.address1_telephone1 = "123123123";
entity.emailaddress1 = "www.bender.com";
entity.lastname = "Contact Name";
parent.$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
datatype: "json",
url: parent.Xrm.Page.context.getClientUrl() + "/api/data/v8.2/contacts",
data: JSON.stringify(entity),
beforeSend: function(XMLHttpRequest) {
XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");
XMLHttpRequest.setRequestHeader("OData-Version", "4.0");
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
async: true,
success: function(data, textStatus, xhr) {
var uri = xhr.getResponseHeader("OData-EntityId");
var regExp = /\(([^)]+)\)/;
var matches = regExp.exec(uri);
var newEntityId = matches[1];
document.getElementById('btnCreateContact').classList.add('disabled');
},
error: function(xhr, textStatus, errorThrown) {
parent.Xrm.Utility.alertDialog(textStatus + " " + errorThrown);
}
});
}
</script>
<meta></head><body style="direction: rtl; overflow-wrap: break-word;">
<div class="container">
<div class="jumbotron">
<h3>Create Contact</h3>
<p>This function creates a new Contact</p>
<button id="btnCreateContact" onclick="fnCreateContact()" type="button" class="btn btn-primary">Create Contact</button>
</div>
</div>
</body></html>
That's all...
In this article we've seen Step by step How to Create a new Entity record using Web API and Javascript in 5 minutes.
Enjoy Microsoft Dynamics 365 CRM!
In this article we've seen Step by step How to Create a new Entity record using Web API and Javascript in 5 minutes.
Enjoy Microsoft Dynamics 365 CRM!
by Carmel Schvartzman
כתב: כרמל שוורצמן
No comments:
Post a Comment