Return to Snippet

Revision: 58311
at July 7, 2012 01:01 by toLL


Updated Code
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;}
   });
}

Revision: 58310
at July 7, 2012 00:30 by toLL


Initial Code
function canQualify(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?$select=StateCode&$filter=LeadId eq guid'" + recordId + "'";
    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) 
           { 
// TODO: narazie mi nie działa więc, zajrzyj do tego później

				var retrievedLeadState = data.d.results[0].StateCode;
				var stateCode = eval(retrievedLeadState.Value);

				if(stateCode == 0)
					return true;
				return false;
           },
       error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + odataSelect); return false;}
   });
}

Initial URL

                                

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

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

Initial Tags
javascript

Initial Language
JavaScript