CRM Crate

Use Xrm.WebApi.offline to perform operations in the Offline Mode of Dynamics 365

We will learn to use the Xrm.WebApi.offline to perform operations in the Offline Mode of Dynamics 365. Before we start, make sure to subscribe to CRM Crate for staying up-to-date.

Xrm.WebApi.offline to perform operations

What is an offline mode in Dynamics 365 CRM?

In Dynamics CRM, the Mobile offline capability allows the users to use the Dynamics 365 for phones app in offline mode to interact with their data, even when the users are not connected to the internet. The Dynamics 365 for phones app provides a rich informative offline experience which helps them to stay productive. Dynamics provides use basic commands such as create, read, update, and delete when the users are offline. Once the users are back online, their changes are automatically synchronized with your Dynamics 365 for phones app. Lets now test the offline mode in our own Dynamics 365 CRM environment.

Click here to learn more about an offiline mode in Dynamics 365.

How to design the Dynamics 365 CRM to perform operations in offline mode?

Mobile Offline allows users to work with the data in offline mode when they are not connected to the internet. Inorder to create or manage the business logics or data programmatically, we need to use Microsoft Dynamics Offline Web API methods within the client side JavaScript.

Using the Xrm.WebApi.offline in JavaScript –

The  Xrm.WebApi.offline client API object provides the following methods.

A. Create a record using Xrm.WebApi. offline.createRecord

createRecord (Client API reference)

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

JavaScript Snippet –

var data =
    {
        "name": "Sample Account",
        "primarycontactid":
        {
            "logicalname": "contact",
            "id": "465b158c-541c-e511-80d3-3863bb347ba8"
        } 
    }

// create account record
Xrm.WebApi.offline.createRecord("account", data).then(
    function success(result) {
        console.log("Account created with ID: " + result.id);
        // perform operations on record creation
    },
    function (error) {
        console.log(error.message);
        // handle error conditions
    }
);

B. Delete a record using Xrm.WebApi.offline.deleteRecord

deleteRecord (Client API reference)

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

JavaScript snippet –

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

C. Validate offline mode using Xrm.WebApi.offline.isAvailableOffline

isAvailableOffline (Client API reference)

Xrm.WebApi.offline.isAvailableOffline(entityLogicalName);

JavaScript snippet –

Xrm.WebApi.offline.isAvailableOffline("account").then(
    function success(result) {
        console.log("Account Is In Offline Mode");
    },
    function (error) {
        console.log(error.message);
        // handle error conditions
    }
);

D. Retrieve a record in offline mode using Xrm.WebApi.offline.retrieveRecord

retrieveRecord (Client API reference)

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

JavaScript snippet –

Xrm.WebApi.offline.retrieveRecord("account", "a8a19cdd-88df-e311-b8e5-6c3be5a8b200", "?$select=name,revenue").then(
    function success(result) {
        console.log("Retrieved values: Name: " + result.name + ", Revenue: " + result.revenue);
        // perform operations on record retrieval
    },
    function (error) {
        console.log(error.message);
        // handle error conditions
    }
);

E. Retrieve multiple records in offline mode using Xrm.WebApi.offline.retrieveMultipleRecords

retrieveMultipleRecords (Client API reference)

Xrm.WebApi.offline.retrieveMultipleRecords(entityLogicalName, options, maxPageSize).then(successCallback, errorCallback);

JavaScript snippet –

var fetchXml = "?fetchXml=<fetch mapping='logical'><entity name='account'><attribute name='accountid'/><attribute name='name'/></entity></fetch>";

Xrm.WebApi.offline.retrieveMultipleRecords("account", fetchXml).then(
    function success(result) {
        for (var i = 0; i < result.entities.length; i++) {
            console.log(result.entities[i]);
        }                    

        // perform additional operations on retrieved records
    },
    function (error) {
        console.log(error.message);
        // handle error conditions
    }
);

F. Update a record in offline mode using Xrm.WebApi.offline.updateRecord

updateRecord (Client API reference)

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

JavaScript snippet –

// define the data to update a record
var data =
    {
        "primarycontactid":
        {
            "logicalname": "contact",
            "id": "61a0e5b9-88df-e311-b8e5-6c3be5a8b200"
        }
    }
// update the record
Xrm.WebApi.offline.updateRecord("account", "5531d753-95af-e711-a94e-000d3a11e605", data).then(
    function success(result) {
        console.log("Account updated");
        // perform operations on record update
    },
    function (error) {
        console.log(error.message);
        // handle error conditions
    }
);

Thus we learned to use the Xrm.WebApi.offline to perform operations in the Offline Mode of Dynamics 365.

5 1 vote
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
error: CRM Crate Security Engine - Disabled Right Click & Selection!

Congratulations!

Well Done,
Welcome to CRM Crate

Stay tuned with us and get all latest updates and learning in Microsoft CRM and related techonologes.