For developers, one of the most exciting new features in Dynamics 365 v9.0 release of Microsoft the new Xrm.WebAPI object which allows JavaScript to perform CRUD and execute SDK messages using the Xrm object.

Preview of the Xrm.WebAPI in Dynamics 365 v9.0

Before and currently, developers need to write the HTTP request or use libraries such as the SDK.Rest.js (available in the SDK) to perform CRUD operations in JavaScript. Now that’s it’s been made part of the Xrm object, executing request is now much easier and straightforward.

Another thing to point out is that the new Xrm.WebAPI now uses Promises and enables the execution of the asynchronous request in a way that resembles synchronous code.

Now let’s see how we can use the Xrm.WebAPI object to execute CRUD operations.

Please note: All information is presented on the preview versions / trial version and not the official released product. As per Microsoft’s discretion the product may change before the official release.

Performing CRUD Operations

Create a record

To create a record, we can use the Xrm.WebApi.createRecord.

The function accepts two parameters, entity type and the object data or the field values for the entity to be created.

Sample usage:

Executing the code above returns the Entity Type and the Guid of the newly created record.

Update a record

Next up is updating a record using Xrm.WebApi.updateRecord. I have updated the newly created account record and used the returned guid.

The update function accepts three parameters:

  1. Entity Type
  2. The ID of the record to be updated
  3. And the object data or fields to be updated

Sample usage:

The updateRecord function returns the Entity Type and the Guid.

 

Retrieve a record

Using Xrm.WebApi.retrieveRecord retrieves a record requires and requires 3 parameters.

  1. Entity Name
  2. Id of the record to be retrieved
  3. Options such as fields to select, filters, etc.

Sample usage:

The function returns the selected record and returns the name column, together with some fields such as the Id and state of the record.

Retrieve multiple records

The function retrieveMultipleRecords accepts 3 parameters:

  1. The Entity Name
  2. Options
  3. And the number of results per page that will be returned

Sample usage:

Upon executing the code, it returned the account we just updated, the returned result also contains the NextLink  property which contains the request URL for the next set of results if there is more depending on the maxPageSize provided.

Delete a record

Sample usage:

Upon execution of the delete function, it returned the entity type and a null ID.

The delete function accepts two parameters, first is the entity type and second is the ID of the record to be deleted.

Conclusion

If you are well familiar with the SDK.Rest javascript library you will notice that the Xrm.WebApi functions are similar to it.

What do you think? On the next part of my blog we will cover more on the available SDK messages that are also available in the new Xrm.WebApi client api.

 

Please note: All information is presented on the preview versions / trial version and not the official released product. As per Microsoft’s discretion the product may change before the official release.