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
1. Xrm.WebApi.retrieveRecord -
To retrieve single record
Parameters - entityname, RecordId, OdataQuery[Select]
2. Xrm.WebApi.retrieveMultipleRecords -
To retrieve the record based on condition
Parameters - entityname, odataQuery [Select, filter]
3. Xrm.WebApi.createRecord -
To create record
Parameters - entityname, data
4. Xrm.WebApi.updateRecord -
To update particular record
Parameters - entityname, RecordId, data
5. Xrm.WebApi.deleteRecord -
To delete particular record.
Parameters - entityname, RecordId
Let's see details use case of above web api's with sample code.
RetrieveRecord -
retrieveRecord is used when we need to retrieve single record and we know the Guid of record.
e.g. we have Feedback form which has student lookup field. So from feedback from we can retrieve student lookup value [Guid] which can be stored in variable and used to retrieve the student record details.
NOTE - When we retrieve lookup value always handle null condition. In case, if feedback form is not having student data and if you pass null value then we will encounter errors.
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);
}
);
}
RetrieveMultiple :
It is used when we don't have recordid but we know the condtion to retrieve the record.
e.g. we have geography table, and we want to retrieve the geography record where name = india and type = country.
RetriveMultiple will always retrun all records which satisfies the condition.
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);
}
);
}
CreateRecord -
It is used to create record using crm JavaScript API.
e.g. Sample code to create contact record
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);
}
);
}
UpdateRecord -
It is used to update record using crm JavaScript API.
e.g. Sample code to update contact record
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);
}
);
}
DeleteRecord -
It is used to delete record using crm JavaScript API.
e.g. Sample code to delete particular contact record
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 more