Dynamics 365 July 2017 Update New Web Api – Part I

Dynamics 365 July 2017 Update New Web Api – Part I

With the release of Dynamics 365 July 2017 Update, Web Api functionality can be performed by calling the functions and properties of the Xrm.WebApi class. The WebApi methods are available for both online and offline access (with offline access being available to mobile clients working offline). The base methods that are available for using the Xrm.WebApi include the following: createRecord, deleteRecord, retrieveRecords, retrieveMultipleRecords, updateRecord, execute and executeMultiple. The following blog post will provide a short description of each and code sample for each one. The execute and execute multiple will be shown in part 2 of this blog article

Xrm.WebApi.createRecord

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

The createRecord method creates a new record in a particular entity, and expects two parameters: the entity name and the new record object, and returns a successCallback function that returns the id of the created record and errorCallback returning an error if such exists. The following sample shows how to use the createRecord function in its basic mode:

var entityName = "account";
var accountData =
    {
        "name": "Contoso, Inc.",
        "accountnumber": "C-5544",
        "address1_line1": "100 Wilshire Blvd",
        "address1_city": "Los Angeles",
        "address1_stateorprovince": "CA",
        "description": "Contoso, Inc was established in 2010",
        "accountcategorycode": 1
    }

// create account record
Xrm.WebApi.createRecord(entityName, accountData).then(
    function success(result) {
        var accountId = result.id;
        createChildRecords(accountId);
    },
    function (error) {
        var alertStrings = { confirmButtonLabel: "Yes", text: error };
        var alertOptions = { height: 200, width: 350 };
        Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
    }
);

Xrm.WebApi.deleteRecord

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

The deleteRecord method delete an existing record in a particular entity, and expects two parameters: the entity name and the unique identifier of the record, and returns a successCallback function that returns the id of the deleted record and errorCallback returning an error if such exists. The following sample shows how to use the deleteRecord function in its basic mode:

var entityName = "account";
var entityId = "5531d753-95af-e711-a94e-000d3a11e605";

Xrm.WebApi.deleteRecord(entityName, entityId).then(
    function success(result) {
        var accountId = result.id;
        var accountName = result.name;
        var message = "Account " + accountName + "was deleted from the system.";
        var alertStrings = { confirmButtonLabel: "Yes", text: message };
        var alertOptions = { height: 200, width: 350 };
        Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
    },
    function (error) {
        var alertStrings = { confirmButtonLabel: "Yes", text: error };
        var alertOptions = { height: 200, width: 350 };
        Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
    }
);

Xrm.WebApi.retrieveRecord

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

The retrieveRecord method retrieves an existing record from a particular entity, and expects three parameters: the entity name, the unique id of the record and an options string (such as particular fields or expanding the results), and returns a successCallback function that returns the retrieved records object and errorCallback returning an error if such exists. The following sample shows how to use the retrieveRecord function in its basic mode:

var entityName = "account";
var entityId = "5531d753-95af-e711-a94e-000d3a11e605";
var options = "?$select=name,accountnumber,description,address1_city,telephone1";

Xrm.WebApi.retrieveRecord(entityName, entityId, options).then(
    function success(result) {
        showResults(result.name, result.accountnumber, result.address1_city);
    },
    function (error) {
        var alertStrings = { confirmButtonLabel: "Yes", text: error };
        var alertOptions = { height: 200, width: 350 };
        Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
    }
);

Xrm.WebApi.retrieveMultipleRecords

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

The retrieveMultipleRecords method retrieves a collection of existing records from a particular entity, and expects three parameters: the entity name, an options string (such as particular fields and filter) and the max page size, and returns a successCallback function that returns the retrieved records object and errorCallback returning an error if such exists. The following sample shows how to use the retrieveRecord function in its basic mode:

var entityName = "account";
var options = "?$select=name,accountnumber,address1_city&$filter=startswith(accountnumber,'C-1')";

Xrm.WebApi.retrieveMultipleRecords(entityName, options, 100).then(
    function success(result) {
        for (var i = 0; i < result.entities.length; i++)
        {
            var name = result.entities[i].name;
            var accountnumber = result.entities[i].accountnumber;
            addToList(name, accountnumber);
        }
    },
    function (error) {
        var alertStrings = { confirmButtonLabel: "Yes", text: error };
        var alertOptions = { height: 200, width: 350 };
        Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
    }
);

Xrm.WebApi.updateRecord

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

The updateRecord method modifies an existing record in a particular entity, and expects three parameters: the entity name, the id of the entity record and the updated record object, and returns a successCallback function that returns the id of the updated record and errorCallback returning an error if such exists. The following sample shows how to use the updateRecord function in its basic mode:

var entityName = "account";
var entityId = "5531d753-95af-e711-a94e-000d3a11e605";
var accountData =
    {
        "address1_line1": "199 Olympic Blvd",
        "address1_line1": "Suite 2330",
        "address1_city": "Los Angeles",
        "address1_stateorprovince": "CA"
    }

Xrm.WebApi.updateRecord(entityName, entityId, accountData).then(
    function success(result) {
        var alertStrings = { confirmButtonLabel: "Yes", text: "Account address has been updated" };
        var alertOptions = { height: 200, width: 350 };
        Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
    },
    function (error) {
        var alertStrings = { confirmButtonLabel: "Yes", text: error };
        var alertOptions = { height: 200, width: 350 };
        Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
    }
);