Prior to V9.0, to perform CRUD operation or to invoke any action, we had to create our own library or use any third party.

Xrm.WebApi helps you to easily perform all these operations and provides methods to use Web API to create, manage records and execute Web API actions and functions.

Properties:

  • online – perform actions or functions when connect to server (online mode).
  • offline – perform actions or functions when offline with mobile clients

Methods:

createRecord Xrm.WebApi.createRecord(entityLogicalName, data).then(successCallback, errorCallback);
Creates a sample account record // define the data to create new account

var data =

{

“name”: “Sample Account”,

“description”: “This is the description of the sample account”

}

// create account record

Xrm.WebApi.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

}

);

 

updateRecord Xrm.WebApi.updateRecord(entityLogicalName, id, data).then(successCallback, errorCallback);
Updates an existing account record // define the data to update a record

var data =

{

“name”: “Updated Sample Account “,

“description”: “This is the updated description of the sample account”

}

// update the record

Xrm.WebApi.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

}

);

 

deleteRecord Xrm.WebApi.deleteRecord(entityLogicalName, id).then(successCallback, errorCallback);
Deletes an account record Xrm.WebApi.deleteRecord(“account”, “5531d753-95af-e711-a94e-000d3a11e605”).then(

function success(result) {

console.log(“Account deleted”);

// perform operations on record deletion

},

function (error) {

console.log(error.message);

// handle error conditions

}

);

 

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

Options:

• $select and $expand, to retrieve your data

Retrieves the name and revenue of an account record Xrm.WebApi.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

}

);

 

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

Options:

• $select, $top, $filter, $expand, and $orderby.

• To specify a FetchXML query, use the fetchXml attribute to specify    the query.

MaxPageSize:

• Specify a positive number that indicates the number of entity records to be returned per page. If you do not specify this parameter, the default value is passed as 5000

Retrieves a collection of account 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

}

);

 

isAvailableOffline Xrm.WebApi.offline.isAvailableOffline(entityLogicalName);
Returns a boolean value indicating whether an entity is present in user’s profile and is currently available for use in offline mode. Xrm.WebApi.offline.isAvailableOffline(“account”)

 

 

execute Xrm.WebApi.online.execute(request).then(successCallback, errorCallback);

 

Request:

Object that will be passed to the Web API endpoint to execute an action, function, or CRUD request. The object exposes a getMetadata method that lets you define the metadata for the action, function or CRUD request you want to execute. The getMetadata method has the following parameters:

·       boundParameter (optional) –

o   undefined – if CRUD operation

o   null – if action or function is not bound to any entity

o   entitylogicalname or entitysetname – if action or function is bound with entity

·       operationName (optional) – Name of action or function or CRUD operation name (“Create”, “Update”, “Delete”, “Retrieve”)

·       operationType (optional) – integer number indicates types of operation

o   0 – action

o   1 – function

o   2 – CRUD

·       parameterTypes – metadata of parameter types. This object has following properties:

o   enumProperties (options) – name, value of enum types

o   structuralProperty – integer number indicates parameter type

–          0 – Unknown

–          1 – PrimitiveType

–          2 – ComplexType

–          3 – EnumerationType

–          4 – Collection

–          5 – EntityType

o   typeName – fully qualified name of parameter type

 

Function to qualify Lead var saleOrderID = null;

var columnsSet = null;

var salesOrderToInvoiceReq = null;

var leadId = “E5975EA3-531C-E511-80D8-3863BB3CE2C8”;

var qualifyLeadReq = “”;

try {

 

qualifyLeadReq = {

entity: {

id: leadId,

entityType: “lead”

},

CreateAccount: true,

CreateContact: true,

CreateOpportunity: true,

Status: 3,

getMetadata: function () {

var metadata = {

boundParameter: “entity”,

parameterTypes: {

“entity”: {

“typeName”: “Microsoft.Dynamics.CRM.lead”,

“structuralProperty”: 5

},

“CreateAccount”: {

“typeName”: “Edm.Int32”,

“structuralProperty”: 1

},

“CreateContact”: {

“typeName”: “Edm.Int32”,

“structuralProperty”: 1

},

“CreateOpportunity”: {

“typeName”: “Edm.Int32”,

“structuralProperty”: 1

},

“Status”: {

“typeName”: “Edm.Int32”,

“structuralProperty”: 1

}

},

operationName: “QualifyLead”,

operationType: 0

};

 

return metadata;

}

};

 

Xrm.WebApi.execute(qualifyLeadReq)

.then(function (result) {

if (result.ok) {

console.log(“Status: %s %s”, result.status, result.statusText);

// perform other operations as required;

}

},

function (error) {

console.log(error.message);

// handle error conditions

}

);

executeMultiple var requests = [req1, req2, req3];

Xrm.WebApi.online.executeMultiple(requests).then(successCallback, errorCallback);

Execute a collection of action, function, or CRUD operations In a transaction:

var changeSet = [req1, req2, req3];

var requests = [changeSet];

Xrm.WebApi.online.executeMultiple(requests).then(successCallback, errorCallback);

 

Mix of individual requests and requests in a transaction

var changeSet = [req1, req2, req3];

var requests = [req4, req5, changeset];

Xrm.WebApi.online.executeMultiple(requests).then(successCallback, errorCallback);

References:

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

https://www.inogic.com/blog/2018/02/dynamics-365-v9-0-xrm-webapi-operations-part-2/