CRUD Operations Using Xrm.WebApi in Dynamics 365 V9.x

In this article, I will show sample code in JavaScript to create, update, delete, retrieve single and retrieve multiple records using Xrm.WebApi. There are two ways to use this web API i.e. Xrm.WebApi.online and Xrm.WebApi.offline. The “online” mode is used when connected to Customer Engagement server in online mode to execute Web API actions and functions. Whereas, offline is for Dynamics 365 for Customer Engagement mobile clients while working in the offline mode.

I have created JS functions with names createRecord, updateRecord, retrieveRecord, deleteRecord, and retrieveMultiple and these will be executed in the same order by calling one after the other completes its execution. As Xrm.WebApi executes in Asynchronous way. So, I am calling each function from inside other functions automatically, starting from one main function i.e. executeCRUDOperationsByWebAPI;

From the function “executeCRUDOperationsByWebAPI”, I am calling “createRecord”. As soon as the function “createRecord” completes its execution, it will call “updateRecord” automatically. Similarly, the function “updateRecord” will call “retrieveRecord” after it completes its execution. Then “deleteRecord” will be called and at the end “retrieveMultiple” executes.

I am showing a code of the first two functions here to keep this article short. The complete code for all CRUD operations can be downloaded from GitHub location from this link. The code file name in this code repository is “WebAPI_CRUD.js”.

function createRecord() {

    var accountInfo = '';
    var accountName = "Alex Ferguson";
    var primaryContactId = "25A17064-1AE7-E611-80F4-E0071B661F01"; //-- Abraham McCormick
    var primaryContactName = "Abraham McCormick";
    var accountCategoryCode = Category.Standard;
    var annualRevenue = 7500000;

    var entityData =
    {
        "name": accountName,
        "revenue": annualRevenue,        // Currency (money type) ---Display Text = Annual Revenue
        "donotphone": false,    //Two Option (boolean type) ---Display Text = Do not allow Phone Calls
        "address1_longitude": 112.512634,     //Floating Point Number (decimal type) --- Display Text = Address 1: Longitude
        "address1_composite": "This is address 1",  //Multiple Lines of Text (memo type) --- Display Text = Address 1
        "accountcategorycode": accountCategoryCode,     // Option Set --- Display Text = Category
        "primarycontactid@odata.bind": "/contacts(" + primaryContactId + ")" //--- Lookup --- Display Text = Primary Contact
    }

    Xrm.WebApi.online.createRecord("account", entityData).then(
                function success(result) {
                    //Success - No Return Data
                    accountId = result.id;
                    var accountInfo = '';
                    accountInfo += 'Account Name = ' + accountName + '\n';
                    accountInfo += 'Primary Contact = ' + primaryContactName + '\n';
                    accountInfo += 'Category = Standard \n';
                    accountInfo += 'Annual Revenue = ' + annualRevenue + '\n';

                    alert("Account created. Details are given below : \n\n" + accountInfo + "\n\n" + "Going to update this record now.");
                    updateRecord();
                    //return accountId;
                },
                function (error) {
                    alert(error.message);
                }
            );
} 

function executeCRUDOperationsByWebAPI() {
    createRecord();
}

Reference: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/clientapi/reference/xrm-webapi

Author: M. Fasih Akbar

I have 12+ years experience in software industry mainly with Microsoft Technology stack. I have 7+ years of experience with ASP.Net. I am working with Microsoft Dynamics CRM since last 4+ years. Throughout my software development career, I have been engaged in project analyses, as well as in designing applications and databases. My major professional expertise belongs to object oriented web-based technologies like Dynamics CRM, Asp.Net, C#, JQuery, Angular JS, Knockout, Javascript, VB.Net, and MVC.

Leave a comment