CRUD Operation Using Xrm.WebApi
Dipali Vaish
September 30, 2018
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.
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:-
Hope this Article will helpful for you. Cheers!!!
0 Comments
Be first to comment on this post.