Hi All Experts ( I will be someday, soon ! ) Here is a question. I wanna get statecode or statuscode of related Pricelevel ( Price List entity ) within the Quote Entity.( Related one ) I have tried JS this within Quote entity( OnLoad) :
var priceLevelStatusCode=Xrm.Page.getAttribute("pricelevelid").getValue()[0].statuscode;
alert(priceLevelStatusCode );
But It displays me undefined in the alert box ! But when I try this :
var priceLevelStatusCode=Xrm.Page.getAttribute("pricelevelid").getValue()[0].name;
alert(priceLevelStatusCode );
It gives me correct name of Price List within the quote. What is the problem? How to get statecode or statuscode of Pricelist?
Update 1 :
I understood that should call OData
, So I did this :
function autoAlarm() {
var pricelevelid = Xrm.Page.getAttribute("pricelevelid").getValue()[0].id;
oDataPath = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organizationdata.svc"
var Odata = oDataPath + "/PricelevelSet?$select=name,statecode&$filter=PricelevelId eq guid'" + pricelevelid + "'";
try {
var retrieveReq = new XMLHttpRequest();
retrieveReq.open("GET", Odata);
retrieveReq.setRequestHeader("Accept", "application/json");
retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
retrieveReq.onreadystatechange = function () {
retrieveReqCallBack(this);
}
retrieveReq.send();
}
catch (err) {
var text = "There was an error on this page.\n\n";
text += "Error description: " + err.message + "\n\n";
text += "Click OK to continue.\n\n";
alert(text);
}
}
function retrieveReqCallBack(retrieveEntityReq) {
var PrefixText = ""
if (retrieveEntityReq.readyState == 4) {
retrieveEntityReq.onreadystatechange = null;
alert(retrieveEntityReq.status);
if (retrieveEntityReq.status == 200) {
var retrievedEntity = JSON.parse(retrieveEntityReq.responseText).d;
alert(retrievedEntity.hasOwnProperty("statuscode"));
alert(retrievedEntity.statuscode);
}
}
}
But now It gives me error 404 ( I think not found ! ). Where I am doing wrong?
Update 2 :
I solved the error number 404.
It was because of case sensitivity.
Now When I am gonna retrieve the value, It display me Undefined
.
Here is my new Code :
function autoAlarm() {
var pricelevel = Xrm.Page.getAttribute("pricelevelid").getValue()[0].id;
var pricelevelid = pricelevel.replace("{","").replace("}","");
oDataPath = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc"
var Odata = oDataPath + "/PriceLevelSet?$select=Name,StateCode&$filter=PriceLevelId eq guid'" + pricelevelid + "'";
try {
var retrieveReq = new XMLHttpRequest();
retrieveReq.open("GET", Odata);
retrieveReq.setRequestHeader("Accept", "application/json");
retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
retrieveReq.onreadystatechange = function () {
retrieveReqCallBack(this);
}
retrieveReq.send();
}
catch (err) {
var text = "There was an error on this page.\n\n";
text += "Error description: " + err.message + "\n\n";
text += "Click OK to continue.\n\n";
alert(text);
}
}
function retrieveReqCallBack(retrieveEntityReq) {
var PrefixText = ""
if (retrieveEntityReq.readyState == 4) {
retrieveEntityReq.onreadystatechange = null;
alert(retrieveEntityReq.status);
if (retrieveEntityReq.status == 200) {
var retrievedEntity = JSON.parse(retrieveEntityReq.responseText).d;
alert(retrievedEntity.hasOwnProperty("StateCode"));
alert(retrievedEntity.StateCode);
}
}
}
It First display false( The code : alert(retrievedEntity.hasOwnProperty("StatusCode"));
and then displays undefiend
.
When I go to the Odata
URL in the browser here what I see about status and status code :
<d:StateCode m:type="Microsoft.Crm.Sdk.Data.Services.OptionSetValue">
<d:Value m:type="Edm.Int32">0</d:Value>
</d:StateCode>
<d:OrganizationId m:type="Microsoft.Crm.Sdk.Data.Services.EntityReference">
<d:Id m:type="Edm.Guid">f52acfb9-78de-402a-a9c2-8229836f0b7e</d:Id>
<d:LogicalName>organization</d:LogicalName>
<d:Name>xxxxx</d:Name>
</d:OrganizationId>
<d:StatusCode m:type="Microsoft.Crm.Sdk.Data.Services.OptionSetValue">
<d:Value m:type="Edm.Int32">100001</d:Value>
</d:StatusCode>
Could Anybody help on this?
(SOLVED) Update 3 :
Finally I could get the statecode
, Here is the code :
function autoAlarm() {
var pricelevel = Xrm.Page.getAttribute("pricelevelid").getValue()[0].id;
var pricelevelid = pricelevel.replace("{","").replace("}","");
oDataPath = Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc"
var Odata = oDataPath + "/PriceLevelSet?$select=Name,StateCode&$filter=PriceLevelId eq guid'" + pricelevelid + "'";
try {
var retrieveReq = new XMLHttpRequest();
retrieveReq.open("GET", Odata);
retrieveReq.setRequestHeader("Accept", "application/json");
retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
retrieveReq.onreadystatechange = function () {
retrieveReqCallBack(this);
}
retrieveReq.send();
}
catch (err) {
var text = "There was an error on this page.\n\n";
text += "Error description: " + err.message + "\n\n";
text += "Click OK to continue.\n\n";
alert(text);
}
}
function retrieveReqCallBack(retrieveEntityReq) {
var PrefixText = ""
if (retrieveEntityReq.readyState == 4) {
retrieveEntityReq.onreadystatechange = null;
if (retrieveEntityReq.status == 200) {
var retrievedEntity = JSON.parse(retrieveEntityReq.responseText).d;
var priceLevelStateCode=retrievedEntity.results[0].StateCode.Value;
alert(priceLevelStateCode);
}
}
}
My mistake was getting Json Array
, the true way is this :
var priceLevelStateCode=retrievedEntity.results[0].StateCode.Value;
Thank you all.