CRM 2011: Retrieve StateCode (Status) using oData (REST) Service Endpoint


/ Published in: JavaScript
Save to your folder(s)

How to retrieve entity StateCode (Status) in JavaScript by accessing CRM oDATA (REST) Service


Copy this code and paste it in your HTML
  1. function getStateCode(recordId)
  2. {
  3. // Service Url
  4. var serverUrl = Xrm.Page.context.getServerUrl();
  5. var dataServiceUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc";
  6.  
  7. // sanitize recordId
  8. recordId = recordId.replace("{", "");
  9. recordId = recordId.replace("}", "");
  10.  
  11. // create select query
  12. var oDataSelect = dataServiceUrl + "/LeadSet?$filter=LeadId eq guid'" + recordId + "'";
  13.  
  14. // create request
  15. $.ajax({
  16. type: "GET",
  17. contentType: "application/json; charset=utf-8",
  18. datatype: "json",
  19. url: oDataSelect,
  20. beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
  21. success: function (data, textStatus, XmlHttpRequest)
  22. {
  23.  
  24. var retrievedLeadState = data.d.results[0].StateCode;
  25.  
  26. // StateCode is an OptionSet so we must use eval-function
  27. var stateCode = eval(retrievedLeadState.Value);
  28.  
  29. return stateCode;
  30. },
  31. error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + odataSelect); return false;}
  32. });
  33. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.