How to Perform Basic CRUD Operations using Offline Web API in Mobile Clients

Sending
User Review
0 (0 votes)

Dynamics 356

Introduction

Microsoft has provided offline ability to use “Dynamics 365 for phones app” or “Field Service (Dynamics 365) App”. Mobile Offline allows users to work with the data in offline mode as well i.e. when they are not connected to the internet. So, in the offline mode, if we want to create or manage the data programmatically, we need to use Microsoft Dynamics Offline Web API methods within the script.

Microsoft Dynamics Offline Web API methods can be used for basic commands while working in offline mode. The basic methods that are available for using the Xrm.WebApi.offline include the following: createRecord,  retrieveRecords, retrieveMultipleRecords, updateRecord and deleteRecord.

Note: These OfflineWebApi methods will work only for tables that are enabled for mobile offline and only basic validation will be performed on the data that is being input.

By using the below method, we can identify whether the entity is available to use in offline mode: Xrm.WebApi.offline.isAvailableOffline(entityLogicalName);

In this blog, I will show the sample codes to create, retrieve, update and delete records using Xrm.WebApi.offline methods in JavaScript.

  1. Create:

It is used to create table record. The required parameters of this function are;

    1. entityLogicalName – Logical name of the table.
    2. data – JSON object of the data to create a new record.

//This function is used to create a contact record
function createContact() {
    var objContact = null;
    try {
          //Create the Json object of contact record
          objContact = new Object();
          objContact["firstname"] = "Jenny";
          objContact["lastname"] = "Smith";
          //Call Create method
          Xrm.WebApi.offline.createRecord("contact", objContact).then(function (result) {
              Xrm.Navigation.openAlertDialog("Contact record has been created successfully.");
          },
        function (error) {
            Xrm.Navigation.openAlertDialog(error.message);
        });
    } catch (e) {
        Xrm.Navigation.openAlertDialog(e.message);
    }

  1. Update:

It is used to update table record. The required parameters of this function are;

    1. entityLogicalName – Logical name of the table.
    2. entityId – GUID of record.
    3. data – JSON object of the data to update record.

//This function is used to update a contact record
function updateContact() {
    var objContact = null;
    try {
        //Create the Json object of contact record
        objContact = new Object();
        objContact["telephone1"] = "18018015012";
        objContact["fax"] = "123456";
        objContact["parentcustomerid_account@odata.bind"] = "/accounts(08f4aca2-9793-eb11-b1ac-0022481cb79b)"
        //Call Update method
        Xrm.WebApi.offline.updateRecord("contact", "fb4231a9-dba4-eb11-b1ac-000d3a4e57c1", objContact).then(function (result) {
            Xrm.Navigation.openAlertDialog("Contact record has been updated successfully.");
        },
      function (error) {
          Xrm.Navigation.openAlertDialog(error.message);
      });
    } catch (e) {
        Xrm.Navigation.openAlertDialog(e.message);
    }
}

  1. Delete:

It is used to delete table record. The required parameters of this function are;

    1. entityLogicalName – Logical name of the table.
    2. entityId – GUID of record.

//This function is used to delete a contact record
function deleteContact() {
    try {
        //Call Delete method
        Xrm.WebApi.offline.deleteRecord("contact", "5d0111d1-bf05-4079-b695-247e4c9acac2").then(function (result) {
            Xrm.Navigation.openAlertDialog("Contact record has been deleted successfully.");
        },
      function (error) {
          Xrm.Navigation.openAlertDialog(error.message);
      });
    } catch (e) {
        Xrm.Navigation.openAlertDialog(e.message);
    }
}

  1. Retrieve:

It is used to retrieve a single record from a table. The required parameters of this function are;

  1. entityLogicalName – Logical name of the table.
  2. entityId – GUID of record.
  3. options – OData Query.

//This function is used to retrieve single contact record
function retrieveContact() {
    try {
        //Call Retrieve method
        Xrm.WebApi.offline.retrieveRecord("contact", "fb4231a9-dba4-eb11-b1ac-000d3a4e57c1", "?$select=firstname,lastname,telephone1").then(function (result) {
            Xrm.Navigation.openAlertDialog("Contact retrieved successfully - FirstName: " + result.firstname + ", LastName: " + result.lastname + " and Telephone: " + result.telephone1);
        },
      function (error) {
          Xrm.Navigation.openAlertDialog(error.message);
      });
    } catch (e) {
        Xrm.Navigation.openAlertDialog(e.message);
    }

  1. RetrieveMultiple:

It is used to retrieve multiple records from the table. The required parameters of this function are;

  1. entityLogicalName – Logical name of the table.
  2. options – we can use OData Query and FetchXML as well.

RetrieveMultiple method with OData Query

//This function is used to retrieve contact records using OData Query
function retrieveContactsWithOdata() {
    try {
        //Call Retrieve method
        Xrm.WebApi.offline.retrieveMultipleRecords("contact", "?$select=firstname,lastname,telephone1&$filter=_parentcustomerid_value ne null").then(function (result) {
            Xrm.Navigation.openAlertDialog("Length: " + result.entities.length);
        },
      function (error) {
          Xrm.Navigation.openAlertDialog(error.message);
      });
    } catch (e) {
        Xrm.Navigation.openAlertDialog(e.message);
    }
}

RetrieveMultiple method with FetchXML – To use a FetchXML, use the fetchXml attribute to specify the query.

//This function is used to retrieve contact records using Fetch XML
function retrieveContactsWithFetch() {
    var fetchXML = null;
    try {
        //Fetch XML to retrieve Contact records
        fetchXML = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
        "<entity name='contact'>" +
          "<attribute name='fullname' />" +
          "<attribute name='telephone1' />" +
          "<attribute name='contactid' />" +
          "<order attribute='fullname' descending='false' />" +
          "<filter type='and'>" +
            "<condition attribute='parentcustomerid' operator='not-null' />" +
          "</filter>" +
        "</entity>" +
      "</fetch>";
        //Call Retrieve method
        Xrm.WebApi.offline.retrieveMultipleRecords("contact", "?fetchXml=" + fetchXML).then(function (result) {
            Xrm.Navigation.openAlertDialog("Length: " + result.entities.length);
        },
      function (error) {
          Xrm.Navigation.openAlertDialog(error.message);
      });
    } catch (e) {
        Xrm.Navigation.openAlertDialog(e.message);
    }
}

Conclusion

By using the Offline Web API methods, we can easily create and manage the table record which is available in the offline database for offline use within mobile clients i.e., “Dynamics 365 for phones app” or “Field Service (Dynamics 365) App”.

Maplytics