Here’s a quick reference guide covering Microsoft Dynamics CRM syntax for common java script.
Most of the examples are provided as functions that you can easily test in the On Load and On Save by generating form Context from execution Context.
Update Record using Xrm.WebApi
function UpdateRecord() {
try {
var entityName = “new_organization”; //Entity Logical Name
var recordId = “919F28C4-F9BB-E911-A977-000D3AF04F8C”; //Guid of the Record
//Data used to Create record
var data = {
“new_organizationname”: “Tata Consultancy Services”,
“new_description”: “This is the description of Tata Consultancy Services”,
“new_noofemployees”: 450000,
“new_revenue”: 30000000
}
Xrm.WebApi.updateRecord(entityName, recordId, data).then(
function success(result) {
Xrm.Utility.alertDialog(“Success”);
},
function (error) {
Xrm.Utility.alertDialog(“Error”);
}
);
}
catch (e) {
Xrm.Utility.alertDialog(e.message);
}
}
Delete Record using Xrm.WebApi
function DeleteRecord() {
try {
var entityName = “new_organization”; //Entity Logical Name
var recordId = “919F28C4-F9BB-E911-A977-000D3AF04F8C”; //Guid of the Record
Xrm.WebApi.deleteRecord(entityName, recordId).then(
function success(result) {
Xrm.Utility.alertDialog(“Success”);
},
function (error) {
Xrm.Utility.alertDialog(“Error”);
}
);
}
catch (e) {
Xrm.Utility.alertDialog(e.message);
}
}
Retrieve Record using Xrm.WebApi
function RetrieveRecord() {
try {
var entityName = “new_organization”; //Entity Logical Name
var recordId = “919F28C4-F9BB-E911-A977-000D3AF04F8C”; //Guid of the Record
var columnsToRetrieve = “$select=new_organizationname, new_noofemployees, new_revenue”; //Columns to Retrieve
Xrm.WebApi.retrieveRecord(entityName, recordId, columnsToRetrieve).then(
function success(result) {
Xrm.Utility.alertDialog(“Success”);
},
function (error) {
Xrm.Utility.alertDialog(“Error”);
}
);
}
catch (e) {
Xrm.Utility.alertDialog(e.message);
}
}
Retrieve Multiple Record using Xrm.WebApi
function RetrieveMultipleRecords() {
try {
var entityName = “new_organization”; //Entity Logical Name
var query = “?$select=new_organizationname, new_noofemployees, new_revenue&$top=3”; //Columns to Retrieve
Xrm.WebApi.retrieveMultipleRecords(entityName, query).then(
function success(result) {
Xrm.Utility.alertDialog(“Success”);
},
function (error) {
Xrm.Utility.alertDialog(“Error”);
}
);
}
catch (e) {
Xrm.Utility.alertDialog(e.message);
}
}
Call Asynchronous Action
function CallAsynchronousAction(executionContext) {
try {
//Get the form context
var formContext = executionContext.getFormContext();
var webApiVersionNumber = “v9.1”;
var serverURL = formContext.context.getClientUrl() + “/api/data/” + webApiVersionNumber + “/”;;
var actionName = “new_customaction”; //Action name
var data = {}; //Action Parameters
if (typeof (data) === “undefined”) {
data = {};
}
var oDataEndPoint = serverURL + actionName;
var req = new XMLHttpRequest();
req.open(“POST”, oDataEndPoint, true); //Action will be invoked Asynchronously
req.setRequestHeader(“Accept”, “application/json”);
req.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
req.setRequestHeader(“OData-MaxVersion”, “4.0”);
req.setRequestHeader(“OData-Version”, “4.0”);
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 204 || this.status === 200) {
if (this.statusText === “No Content” || this.statusText === “”) // In case of 204
var response = req.response;
else {
var response = JSON.parse(req.response);
}
} else {
var error = JSON.parse(req.response).error;
Xrm.Utility.alertDialog(error.message);
}
}
};
req.send(JSON.stringify(data));
}
catch (e) {
Xrm.Utility.alertDialog(e.message);
}
}
Call Synchronous Action
function CallSynchronousAction(executionContext) {
try {
//Get the form context
var formContext = executionContext.getFormContext();
var webApiVersionNumber = “v9.1”;
var serverURL = formContext.context.getClientUrl() + “/api/data/” + webApiVersionNumber + “/”;;
var actionName = “new_customaction”; //Action name
var data = {}; //Action Parameters
if (typeof (data) === “undefined”) {
data = {};
}
var oDataEndPoint = serverURL + actionName;
var req = new XMLHttpRequest();
req.open(“POST”, oDataEndPoint, false); //Action will be invoked synchronously
req.setRequestHeader(“Accept”, “application/json”);
req.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
req.setRequestHeader(“OData-MaxVersion”, “4.0”);
req.setRequestHeader(“OData-Version”, “4.0”);
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 204 || this.status === 200) {
if (this.statusText === “No Content” || this.statusText === “”) // In case of 204
var response = req.response;
else {
var response = JSON.parse(req.response);
}
} else {
var error = JSON.parse(req.response).error;
Xrm.Utility.alertDialog(error.message);
}
}
};
req.send(JSON.stringify(data));
}
catch (e) {
Xrm.Utility.alertDialog(e.message);
}
}
Associate request using Web Api
function AssociateRequest(executionContext) {
try {
//Get the form context
var formContext = executionContext.getFormContext();
var serverURL = formContext.context.getClientUrl();
var currentEntityPlurarName = “fieldsecurityprofiles”;
var currentEntityId = “4E7C654C-7150-E711-811F-C4346BACBA84”;//Get Field Security Profile id
var relationShipName = “systemuserprofiles_association”;
var otherEntityPlurarName = “systemusers”;
var otherEntityId = “2F5FC0F6-F247-E811-810F-C4346BDCF131”; //Get System User id
var associate = {}
associate[“@odata.id”] = serverURL + “/api/data/v9.1/” + otherEntityPlurarName + “(” + otherEntityId + “)”;
var req = new XMLHttpRequest();
req.open(“POST”, serverURL + “/api/data/v9.1/” + currentEntityPlurarName + “(” + currentEntityId + “)/” + relationShipName + “/$ref”, false);
req.setRequestHeader(“Accept”, “application/json”);
req.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
req.setRequestHeader(“OData-MaxVersion”, “4.0”);
req.setRequestHeader(“OData-Version”, “4.0”);
req.onreadystatechange = function () {
if (this.readyState == 4 /* complete */) {
req.onreadystatechange = null;
if (this.status == 204) {
//Success
} else {
var error = JSON.parse(this.response).error;
Xrm.Utility.alertDialog(error.message);
}
}
};
req.send(JSON.stringify(associate));
}
catch (e) {
Xrm.Utility.alertDialog(e.message);
}
}