CRUD (create, read, update, delete) operations using the Xrm.WebApi.online.execute
and Xrm.WebApi.online.executeMultiple
methods of the Dynamics 365 Client API, with JavaScript, are not very well documented in the official Microsoft documentation. The Xrm.WebApi.online.execute (Client API reference) documentation has no code samples for CRUD opertations.
So, here are code samples to get you going…
CRUD Opertations with Xrm.WebApi.online.execute
Below are some code samples for running CRUD operations using the Xrm.WebApi.online.execute method.
Create
var Sdk = window.Sdk || {}; | |
/** | |
* Request to create a record | |
* @param {string} entityTypeName - The entity type name. | |
* @param {Object} payload - The entity payload. | |
*/ | |
Sdk.CreateRequest = function (entityTypeName, payload) { | |
this.etn = entityTypeName; | |
this.payload = payload; | |
this.getMetadata = function () { | |
return { | |
boundParameter: null, | |
parameterTypes: {}, | |
operationType: 2, | |
operationName: "Create", | |
}; | |
}; | |
}; | |
// Construct a request object from the metadata | |
var request = new Sdk.CreateRequest("new_entity", { "new_name": "Foo Bar" }); | |
// Use the request object to execute the function | |
Xrm.WebApi.online.execute(request).then( | |
(result) => { | |
if (result.ok) { | |
console.log(`${result.status} ${result.statusText}`); | |
} | |
}, | |
(error) => { | |
console.log(error.message); | |
} | |
); |
Retrieve
var Sdk = window.Sdk || {}; | |
/** | |
* Request to update a record | |
* @param {Object} entityReference - The entity reference. | |
* @param {Array} columns - The columns. | |
*/ | |
Sdk.RetrieveRequest = function (entityReference, columns) { | |
this.entityReference = entityTypeName; | |
this.columns = columns; | |
this.getMetadata = function () { | |
return { | |
boundParameter: null, | |
parameterTypes: {}, | |
operationType: 2, | |
operationName: "Retrieve", | |
}; | |
}; | |
}; | |
// Construct a request object from the metadata | |
var request = new Sdk.RetrieveRequest({ etn: "new_entity", id: "00000000-0000-0000-0000-000000000000" }, ["new_name"]); | |
// Use the request object to execute the function | |
Xrm.WebApi.online.execute(request).then( | |
(result) => { | |
if (result.ok) { | |
console.log(`${result.status} ${result.statusText}`); | |
} | |
}, | |
(error) => { | |
console.log(error.message); | |
} | |
); |
Update
var Sdk = window.Sdk || {}; | |
/** | |
* Request to update a record | |
* @param {string} entityTypeName - The entity type name. | |
* @param {string} id - The record ID. | |
* @param {Object} payload - The entity payload. | |
*/ | |
Sdk.UpdateRequest = function (entityTypeName, id, payload) { | |
this.etn = entityTypeName; | |
this.id = id; | |
this.payload = payload; | |
this.getMetadata = function () { | |
return { | |
boundParameter: null, | |
parameterTypes: {}, | |
operationType: 2, | |
operationName: "Update", | |
}; | |
}; | |
}; | |
// Construct a request object from the metadata | |
var request = new Sdk.UpdateRequest("new_entity", "00000000-0000-0000-0000-000000000000", { "new_name": "Foo Bar" }); | |
// Use the request object to execute the function | |
Xrm.WebApi.online.execute(request).then( | |
(result) => { | |
if (result.ok) { | |
console.log(`${result.status} ${result.statusText}`); | |
} | |
}, | |
(error) => { | |
console.log(error.message); | |
} | |
); |
Delete
var Sdk = window.Sdk || {}; | |
/** | |
* Request to delete a record | |
* @param {Object} entityReference - The entity reference. | |
*/ | |
Sdk.DeleteRequest = function (entityReference) { | |
this.entityReference = entityReference; | |
this.getMetadata = function () { | |
return { | |
boundParameter: null, | |
parameterTypes: {}, | |
operationType: 2, | |
operationName: "Delete", | |
}; | |
}; | |
}; | |
// Construct a request object from the metadata | |
var request = new Sdk.DeleteRequest({ entityType: "new_entity", id: "00000000-0000-0000-0000-000000000000" }); | |
// Use the request object to execute the function | |
Xrm.WebApi.online.execute(request).then( | |
(result) => { | |
if (result.ok) { | |
console.log(`${result.status} ${result.statusText}`); | |
} | |
}, | |
(error) => { | |
console.log(error.message); | |
} | |
); |
CRUD Opertations with Xrm.WebApi.online.executeMultiple
Below are some of code samples for running CRUD operations using the Xrm.WebApi.online.executeMultiple method.
Note: As per the OData v4 specifications, you cannot include read operations as part of a change set.
Create
var Sdk = window.Sdk || {}; | |
/** | |
* Request to create a record | |
* @param {string} entityTypeName - The entity type name. | |
* @param {Object} payload - The entity payload. | |
*/ | |
Sdk.CreateRequest = function (entityTypeName, payload) { | |
this.etn = entityTypeName; | |
this.payload = payload; | |
this.getMetadata = function () { | |
return { | |
boundParameter: null, | |
parameterTypes: {}, | |
operationType: 2, | |
operationName: "Create", | |
}; | |
}; | |
}; | |
// Construct a request object array from the metadata | |
var requests = [ | |
new Sdk.CreateRequest("new_entity", { "new_name": "Foo" }), | |
new Sdk.CreateRequest("new_entity", { "new_name": "Bar" }) | |
]; | |
// Use the request object array to execute the function | |
Xrm.WebApi.online.executeMultiple(requests).then( | |
(result) => { | |
if (result.ok) { | |
console.log(`${result.status} ${result.statusText}`); | |
} | |
}, | |
(error) => { | |
console.log(error.message); | |
} | |
); |
Retrieve
var Sdk = window.Sdk || {}; | |
/** | |
* Request to update a record | |
* @param {Object} entityReference - The entity reference. | |
* @param {Array} columns - The columns. | |
*/ | |
Sdk.RetrieveRequest = function (entityReference, columns) { | |
this.entityReference = entityTypeName; | |
this.columns = columns; | |
this.getMetadata = function () { | |
return { | |
boundParameter: null, | |
parameterTypes: {}, | |
operationType: 2, | |
operationName: "Retrieve", | |
}; | |
}; | |
}; | |
// Construct a request object array from the metadata | |
var requests = [ | |
new Sdk.RetrieveRequest({ etn: "new_entity", id: "00000000-0000-0000-0000-000000000000" }, ["new_name"]), | |
new Sdk.RetrieveRequest({ etn: "new_entity", id: "00000000-0000-0000-0000-000000000000" }, ["new_name"]) | |
]; | |
// Use the request object array to execute the function | |
Xrm.WebApi.online.executeMultiple(requests).then( | |
(result) => { | |
if (result.ok) { | |
console.log(`${result.status} ${result.statusText}`); | |
} | |
}, | |
(error) => { | |
console.log(error.message); | |
} | |
); |
Update
var Sdk = window.Sdk || {}; | |
/** | |
* Request to update a record | |
* @param {string} entityTypeName - The entity type name. | |
* @param {string} id - The record ID. | |
* @param {Object} payload - The entity payload. | |
*/ | |
Sdk.UpdateRequest = function (entityTypeName, id, payload) { | |
this.etn = entityTypeName; | |
this.id = id; | |
this.payload = payload; | |
this.getMetadata = function () { | |
return { | |
boundParameter: null, | |
parameterTypes: {}, | |
operationType: 2, | |
operationName: "Update", | |
}; | |
}; | |
}; | |
// Construct a request object array from the metadata | |
var requests = [ | |
new Sdk.UpdateRequest("new_entity", "00000000-0000-0000-0000-000000000000", { "new_name": "Foo" }), | |
new Sdk.UpdateRequest("new_entity", "00000000-0000-0000-0000-000000000000", { "new_name": "Bar" }) | |
]; | |
// Use the request object array to execute the function | |
Xrm.WebApi.online.executeMultiple(requests).then( | |
(result) => { | |
if (result.ok) { | |
console.log(`${result.status} ${result.statusText}`); | |
} | |
}, | |
(error) => { | |
console.log(error.message); | |
} | |
); |
Delete
var Sdk = window.Sdk || {}; | |
/** | |
* Request to delete a record | |
* @param {Object} entityReference - The entity reference. | |
*/ | |
Sdk.DeleteRequest = function (entityReference) { | |
this.entityReference = entityReference; | |
this.getMetadata = function () { | |
return { | |
boundParameter: null, | |
parameterTypes: {}, | |
operationType: 2, | |
operationName: "Delete", | |
}; | |
}; | |
}; | |
// Construct a request object array from the metadata | |
var requests = [ | |
new Sdk.DeleteRequest({ entityType: "new_entity", id: "00000000-0000-0000-0000-000000000000" }), | |
new Sdk.DeleteRequest({ entityType: "new_entity", id: "00000000-0000-0000-0000-000000000000" }) | |
]; | |
// Use the request object array to execute the function | |
Xrm.WebApi.online.executeMultiple(requests).then( | |
(results) => { | |
console.log(`${result.status} ${result.statusText}`); | |
}, | |
(error) => { | |
console.log(error.message); | |
} | |
); |