Sam's Microsoft Dynamics 365 Blog

Wednesday, 5 September 2018

CRUD Operation Using Xrm.WebApi



In Dynamics 365 One of the important enhancement is Xrm.WebApi. Dynamics 365 v9.0 which will help to make developers life simple. We can perform below operations in Xrm.WebApi.

Method
Description
createRecord
Creates an entity record.
deleteRecord
Deletes an entity record.
retrieveRecord
Retrieves an entity record.
retrieveMultipleRecords
Retrieves a collection of entity records.
updateRecord
Updates an entity record.
isAvailableOffline
Returns a Boolean value indicating whether an entity is present in user’s profile and is currently available for use in offline mode.
execute
Execute a single action, function, or CRUD operation.
executeMultiple
Execute a collection of action, function, or CRUD operations.



Note: Supports Dynamics 365 (online), version 9.x

Create

Create a record in an entity.

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

Example:-
// define the data to create new account
var _accountData =
    {
        "name": "Sam test",
        "creditonhold": false,
        "address1_latitude": 47.639583,
        "description": "Example of WebApi",
        "revenue": 1000000,
        "accountcategorycode": 1
    }

// create account record
Xrm.WebApi.createRecord("account", accountData).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
    }
);
Response:-




Retrieve

Retrieve a single record of an entity.

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

Example:-
// retrieve a single record
Xrm.WebApi.retrieveRecord("account", "4263f467-f9af-e811-a95c-000d3a3780dd", "?$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
    }
);

Response:-




Retrieve Multiple Records

Retrieve a collection of records from an entity.

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

Example:-
// retrieve multiple records
Xrm.WebApi.retrieveMultipleRecords("account", "?$select=name&$top=3").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
    }
);

Response:-





Update Records

Update a record of an entity.

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

Example:-
// define the data to update a record
var _accountUpdateData =
    {
        "name": "Updated Sam test Account ",
        "creditonhold": true,
        "address1_latitude": 47.639583,
        "description": "This is the updated description of the sample",
        "revenue": 6000000,
        "accountcategorycode": 2
    }
// update the record
Xrm.WebApi.updateRecord("account", "4263f467-f9af-e811-a95c-000d3a3780dd", _accountUpdateData).then(
    function success(result) {
        console.log("Account updated");
        // perform operations on record update
    },
    function (error) {
        console.log(error.message);
        // handle error conditions
    }
);

Response:-




Delete Records

Delete a record of an entity.

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

Example:-
// delete an account record
Xrm.WebApi.deleteRecord("account", "4263f467-f9af-e811-a95c-000d3a3780dd").then(
    function success(result) {
        console.log("Account deleted");
        // perform operations on record deletion
    },
    function (error) {
        console.log(error.message);
        // handle error conditions
    }
);

Response:-




No comments:

Post a Comment

Web Resource vs PCF vs Canvas App - which of the one is used?

While started working on specific Business Requirements related to custom layout, there is a always common question that "where to star...