/ Published in: JavaScript
How to retrieve entity StateCode (Status) in JavaScript by accessing CRM oDATA (REST) Service
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function getStateCode(recordId) { // Service Url var serverUrl = Xrm.Page.context.getServerUrl(); var dataServiceUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc"; // sanitize recordId recordId = recordId.replace("{", ""); recordId = recordId.replace("}", ""); // create select query var oDataSelect = dataServiceUrl + "/LeadSet?$filter=LeadId eq guid'" + recordId + "'"; // create request $.ajax({ type: "GET", contentType: "application/json; charset=utf-8", datatype: "json", url: oDataSelect, beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); }, success: function (data, textStatus, XmlHttpRequest) { var retrievedLeadState = data.d.results[0].StateCode; // StateCode is an OptionSet so we must use eval-function var stateCode = eval(retrievedLeadState.Value); return stateCode; }, error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + odataSelect); return false;} }); }