With Microsoft releasing Dynamics 365 update 9.0, now, we have a new library to implement WebAPI methods using Xrm.WebApi. Instead of writing the complete request, now we can just use direct the CRUD methods from the Web API.
If you are looking for how to write Web API for Dynamics CRM 2016, check out the sample index here.
Xrm.WebApi has two properties to use for the Online and the Offline client respectively. In this article, we are going to implement a sample HTML web resource using Xrm.WebApi for the online client.
Now, to create an entity record, we can simply call Xrm.WebApi Create method using the following parameters.
- function createAccount() {
-
- var data = {
- "name": document.getElementById("txtname").value,
- "address1_city": document.getElementById("txtcity").value,
- "telephone1": document.getElementById("txtphone").value,
- "numberofemployees": document.getElementById("txttotalemployees").value,
- "websiteurl": document.getElementById("txtwebsite").value
-
- }
-
- parent.Xrm.WebApi.createRecord("account", data).then(
- function success(result) {
- document.getElementById("txtaccountid").value = result.id;
- alert("Account Created !!");
- },
- function(error) {
- alert(error.message);
- }
- );
- }
To update the entity record, we can use the following method and pass the updated data.
- function updateAccount() {
-
- var data = {
- "name": document.getElementById("txtname").value,
- "address1_city": document.getElementById("txtcity").value,
- "telephone1": document.getElementById("txtphone").value,
- "numberofemployees": document.getElementById("txttotalemployees").value,
- "websiteurl": document.getElementById("txtwebsite").value
-
- }
-
- var accountId = document.getElementById("txtaccountid").value;
-
-
- parent.Xrm.WebApi.updateRecord("account", accountId, data).then(
- function success(result) {
- alert("Account Record Updated");
-
- },
- function(error) {
- alert(error.message);
-
- }
- );
-
- }
And, to delete, we can simply pass the entity and record it like following.
- function deleteAccount() {
- var accountId = document.getElementById("txtaccountid").value;
- parent.Xrm.WebApi.deleteRecord("account", accountId).then(
- function success(result) {
- alert("Account deleted");
-
- },
- function(error) {
- alert(error.message);
-
- }
- );
- }
You can download the complete code from
GitHub here. Stay tuned for more Dynamics 365 update 9.0 features !!!