JavaScript WebAPI [Xrm.webApi]
In this blog we will see usecase of Xrm.WebApi. Generally, it is used to do CRUD operation in dynamics CRM.
Most of the times we will be using retrievemultiple, retrievesingle and updaterecord.All these web api's are asynchronous, that means once we execute api, it wiill not wait for response. It will execute remaining code till we get response from api.
The sample code for webapis can be generated by Dataverse REST Builder tool from xrmtoobox.
We will see below use case of WebApi
Let's see details use case of above web api's with sample code.
RetrieveRecord -
function RetrieveCRMRecord(executionContext) {
var formContext = executionContext.getFormContext();
//Parameters - Entityname, Guid of record, fields that needs to retrieve
// Entityname - cr02e_student
//Record ID - ee669047-e897-e311-a5a7-d89d67633dbc
//odata query - we will pass field that needs to be retrieve from record
Xrm.WebApi.retrieveRecord(
"cr02e_student",
"ee669047-e897-e311-a5a7-d89d67633dbc",
"?$select=cr02e_branchname,cr02e_dob,cr02e_email").then(
function success(result) {
var cr02e_studentid = result["cr02e_studentid"];
//choice field. returns optionset code
var cr02e_branchname = result["cr02e_branchname"];
//returns label of optionset
var cr02e_branchname_formatted =
result["[email protected]"];
var cr02e_dob = result["cr02e_dob"]; // Date Time
//returns friendly date string 7/4/2025
var cr02e_dob_formatted =
result["[email protected]"];
var cr02e_email = result["cr02e_email"]; // Text
var cr02e_coursename = result["_cr02e_coursename_value"]; // Lookup guid
//returns name of lookup record
var cr02e_coursename_formatted =
result["_cr02e_coursename_value@OData.Community.Display.V1.FormattedValue"];
},
function(error) {
console.log(error.message);
}
);
}
function RetrieveMultipleCRMRecord(executionContext) {
var formContext = executionContext.getFormContext();
//select is used to mention which fields you want to retrieve
var selectColumns = "cr02e_name,cr02e_type";
//filter is used to mention criteria to retrieve records
var filterConditions = "cr02e_name eq 'India' and cr02e_type eq 179400002 and statecode eq 0";
var odataQuery = "?$select=" + selectColumns + "&$filter=(" + filterConditions + ")";
//Parameters - Entityname, Guid of record, fields that needs to retrieve
// Entityname - cr02e_geography
//odata Query
Xrm.WebApi.retrieveMultipleRecords(
"cr02e_geography",
odataQuery
).then(
function success(results) {
console.log(results);
for (var i = 0; i & lt; results.entities.length; i++) {
var result = results.entities[i];
// Columns
var cr02e_geographyid = result["cr02e_geographyid"]; // Guid
var cr02e_name = result["cr02e_name"]; // Text
var cr02e_type = result["cr02e_type"]; // Choice
var cr02e_type_formatted = result["[email protected]"];
}
},
function(error) {
console.log(error.message);
}
);
}
function CreateContactRecord(executionContext) {
var formContext = executionContext.getFormContext();
var data = {};
record.firstname = "Abhishek"; // Text
record.lastname = "Dhandare"; // Text
record.emailaddress1 = "[email protected]"; // Text
record.familystatuscode = 1; // Choice
record["[email protected]"] =
"/accounts(6b29fc40-ca47-1067-b31d-00dd010662da)"; // Lookup - tablename(Guid)
record.birthdate = "2025-03-04"; // Date Time
//entityname, data
Xrm.WebApi.createRecord("contact", data).then(
function success(result) {
var newId = result.id;
console.log(newId);
},
function(error) {
console.log(error.message);
}
);
}
function UpdateContactRecord(executionContext) {
var formContext = executionContext.getFormContext();
var updateDate = {};
record.firstname = "Abhishek"; // Text
record.lastname = "Dhandare"; // Text
record.emailaddress1 = "[email protected]"; // Text
record.familystatuscode = 1; // Choice
record["[email protected]"] = "/accounts(6b29fc40-ca47-1067-b31d-00dd010662da)"; // Lookup
record.birthdate = "2025-03-04"; // Date Time
// Entityname
// Guid to update particular record
// data that needs to be updated
Xrm.WebApi.updateRecord(
"contact",
"6b29fc40-ca47-1067-b31d-00dd010662da",
updateDate
).then(
function success(result) {
var updatedId = result.id;
console.log(updatedId);
},
function(error) {
console.log(error.message);
}
);
}
function DeleteContactRecord(executionContext) {
//entityname
//Guid of record that needs to be deleted
Xrm.WebApi.deleteRecord(
"contact",
"6b29fc40-ca47-1067-b31d-00dd010662da"
).then(
function success(result) {
console.log(result);
},
function(error) {
console.log(error.message);
}
);
}
Published on:
Learn moreWe can help you with JavaScript WebAPI [Xrm.webApi]
If you want help implementing, troubleshooting, or improving this product, contact us and we’ll point you in the right direction.
Related posts
🚀 CRM ↔ ERP Real-Time Integration Framework
🚀 CRM ↔ ERP Real-Time Integration FrameworkSharing a simple and reliable pattern to implement bi-directional synchronization between Mi...
Security Model of Dynamics CRM
Business Unit – It is a way to group business activities.When an organization is created, a Root Business Unit is created by default. Thi...
Power Automate - [Dataverse]
Power Automate is used to automate operations through triggers and actions.Triggers: Added, Modified, Deleted, Manual (Instant), ScheduledActi...
Bulk insert CRM Data into SQL table using sqlbulkcopy
If you're in need of bulk inserting CRM data into a SQL table, this tutorial provides a step-by-step guide that will get you there with minima...
Most Used Plugin Syntax in Dynamics 365 CRM
For Dynamics 365 CRM users, plugins are essential tools that allow for the customization of business rules and processes. This tutorial delves...
Get option-set value from text and get option-set text from Value using plugin
If you're looking for a way to extract option-set values from text or vice versa, this plugin tutorial is just what you need. By following the...