Inkey Solution Logo
banner

Blogs

Microsoft Dynamics 365 v9.0 – Xrm.WebApi methods for CRUD operations

, February 12, 2018 7006 Views

Microsoft Dynamics 365 V9 is provided with the new library to implement the Web API methods. The basic CRUD operations can be performed easily using these methods.

Previously, developers used Web API requests to perform CRUD operations in JavaScript. But now, the Xrm.WebApi has eased the process.

In this blog, I will explain how to use the methods of the Xrm.WebApi to perform CRUD operations.

Create method :

This method is used for retrieving the records of a Lead entity.

Syntax :

Xrm.WebApi.createRecord(entityLogicalName, data).then(successCallback, errorCallback);

Now, let me explain you the parameters of createRecord method:-

1. EntityLogicalName : The name of the entity for which the record is to be created.

2. data : It is a JSON object which defines the attributes and values for the new record.

3. successCallback : It is the function to be called on the success of record creation, which takes the object with newly created record id(string) and its logical name(string) as parameters.

4. ErrorCallback : Function to be called on the failure of record creation, which takes the object errorCode(Number) and related message(string) as parameters.

Below is the JavaScript code, that, I have called on “On Change” event of “Mobile Phone” field of the Lead entity, which will create a lead record.

function createRecord() {
 // define the data to create new lead 
 var data =
 {
 "firstname": "Saira",
 "jobtitle": "Manager",
 "telephone1": "999999999",
 "emailaddress1": "abc@xyz.com"
 }
 // create lead record
 Xrm.WebApi.createRecord("lead", data).then(function success(result)
 {
 alert("Lead created with ID: " + result.id);
 // implement the logic here
 },
 function (error) {
 alert(error.message);
 }
 ); // end of function
}

Retrieve method :

This method is used for retrieving the records of an entity.

Syntax :

Xrm.WebApi.retrieveRecord(entityLogicalName, id, options).then(successCallback, errorCallback);

Below are the parameters of retrieveRecord method:-

1. id : It is the GUID of the record that has to be retrieved

2. options : There are two options provided to query – $select and $expand

$select is used to retrieve the required attributes.

$expand is used to retrieve the related entity attributes.

And, there are also EntityLogicalName, successCallback & ErrorCallback function as parameters for this method, same as in createRecord method.

Below is the JavaScript code, that I have called on “On Change” event of “City” field of the Lead entity, which will retrieve the lead record having specified GUID as it’s ID and the related entity record attributes using $expand option.

Here, transactioncurrencyid is the lookup field on lead entity(related to currency entity), which is accessed using the $expand option, that returns the required attributes like currencyname and currencysymbol.

So, the resulting record will have the firstname of the lead, the related currency name & the currency symbol in it.

function retrieveRecord() {
Xrm.WebApi.retrieveRecord("lead”,"00000000-0000-0000-0000-000000000000", "$select=firstname&$expand=transactioncurrencyid($select=currencyname,currencysymbol)").then(function success(result)
{
 alert("Retrieved values: Name: " + result.firstname +
 "Currency Name: " + result.transactioncurrencyid.currencyname +
 "Currency Symbol: " + result.transactioncurrencyid.currencysymbol);
 // implement the logic here
 },
 function (error) {
 alert(error.message);
 }
 ); // end of function
}

Update method :

This method is used for updating the record of a Lead entity.

Syntax:

Xrm.WebApi.updateRecord(entityLogicalName, id, data).then(successCallback, errorCallback);

The parameters for this method are EntityLogicalName, id, data, successCallback & ErrorCallback.

Below is the JavaScript code, that, I have called on “On Change” event of “Company Name” field of the Lead entity, which will update the record having specified GUID as it’s ID.

function updateRecord() {
// define the data to update a record
 var data =
 {
 "firstname": "James",
 "jobtitle": "Product Analyst",
 "telephone1": "999999999",
 "emailaddress1": "abc@xyz.com" 
}
 // update the record
 Xrm.WebApi.updateRecord("lead", "00000000-0000-0000-0000-000000000000", data).then(function success(result)
 {
 alert("Lead updated.");
 // implement the logic here
 },
 function (error) {
 alert(error.message);
 }
); // end of function
}

Delete method :

This method is used to delete the record of a Lead entity.

Syntax :

Xrm.WebApi.deleteRecord(entityLogicalName, id).then(successCallback, errorCallback);

The parameters for this method are EntityLogicalName, id, successCallback & ErrorCallback.

Below is the JavaScript code, that I have called on “On Change” event of “Email” field of the Lead entity, which will delete the lead record having specified GUID as it’s ID.

function deleteRecord() {
 Xrm.WebApi.deleteRecord("lead", "00000000-0000-0000-0000-000000000000").then(function success(result)
 {
 alert("Lead deleted");
 // implement the logic here
 },
 function (error) {
 alert(error.message);
 }
 ); // end of function
}

In this way, you can do the CRUD operations in CRM using the Xrm.WebApi methods very easily.

I hope this helps you!!

Happy CRMing.


 

Insert data into Many-to-Many relationship in Dynamics CRM very easily & quickly, using the Drag and drop listbox.
http://www.inkeysolutions.com/what-we-do/dynamicscrmaddons/drag-and-drop-listbox

mm

Inkey

INKEY is your solution partner.
Our focus is to deliver you in-time intelligent innovative solutions ("key") for the problems in hand. Maintaining a quality standard right from the inception of a project is our top most priority.

Our team of talented professionals will execute your projects with dedication and excellence. We take ownership and accountability for the effort that goes into meeting our client’s needs.

Years of experience and proven success of delivering innovative custom solutions.

More posts by

Leave a Reply

Your email address will not be published. Required fields are marked *

The maximum upload file size: 2 MB. You can upload: image, audio, video, document, spreadsheet, interactive, text, archive, code, other. Drop file here

Would you like to digitize your business and put it on the cloud?
Do you need clear, concise reports for your organization?