In version 9.0, we don’t need to create any request for using Web API, instead, we will use built-in and pre-defined functions for using Web API. Now Dynamics 365 has a new library Xrm.WebApi and performs CRUD using Web API.
Retrieving record
Check Microsoft URL
var accountId = "DC1F674E-55F1-E711-A95F-000D3AF27163";
Xrm.WebApi.retrieveRecord("account", accountId, "?$select=name,revenue").then(
function success(result) {
alert("Retrieved values: Name: " + result.name + ", Revenue: " + result.revenue);
// perform operations on record retrieval
},
function (error) {
alert(error.message);
// handle error conditions
}
);
Retrieving records
Check Microsoft URL
Xrm.WebApi.retrieveMultipleRecords("contact", "?$select=firstname,lastname&$top=3&$filter=age lt 50").then(
function success(result) {
for (var i = 0; i < result.entities.length; i++) {
alert(result.entities[i]);
}
// perform additional operations on retrieved records
},
function (error) {
alert(error.message);
// handle error conditions
}
);
Retrieving records by fetch XML
function retrieveMultipleContacts(executioncontext) {
var formContext = executioncontext.getFormContext();
var contactId = formContext.data.entity.getId();
var fetchContacts = "<fetch version='1.0' output-format='xmlplatform'
mapping='logical' distinct='false'>" +
"<entity name='contact' >" +
"<attribute name='fullname' />" +
"<attribute name='telephone1' />" +
"<attribute name='contactid' />" +
"<order attribute='fullname' descending='false' />" +
"<filter type='and'>" +
"<condition attribute='parentcustomerid' operator='eq'
uitype='account' value ='" + accountId + "' />" +
"</filter>" +
"</entity >" +
"</fetch > ";
Xrm.WebApi.retrieveMultipleRecords("contact", "fetchXml= " +
fetchContacts).then(
function success(result) {
for (var i = 0; i < result.entities.length; i++) {
alert(result.entities[i]);
}
},
function (error) {
alert(error.message);
});
}