1

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.

Alireza
  • 179
  • 1
  • 2
  • 11

2 Answers2

0

Pricelevel is a lookup in quote, you can get Id & name (display text) of that related entity record using Xrm.Page.getAttribute("pricelevelid").getValue()[0]. For any other attributes (ex.statecode) you have to make a web api call to get it from Database using the Id key.

Update:
You can use 2011 compatible CRM REST Builder to build queries, test & exact snippet can be utilized in CRM form js library. Download & import this managed solution in Dev CRM instance & right away build queries.

OrganizationData.svc can be used & retrieve the necessary attributes by wrapping in XMLHttpRequest. Read more

Sample:

var pricelevelid = Xrm.Page.getAttribute("pricelevelid").getValue()[0].id;

var Odata = oDataPath + "/PriceLevelSet?$select=name,statecode&$filter=PriceLevelId eq guid'" + pricelevelid + "'";
0

Lookup controls contain the ID and Name of the target entity.

In your case the Lookup control is the pricelevelid control

If you want more information from the related Price Level record, you'll need to to retrieve the specific record.


To do this in CRM 365

Here's the link to the MSDN Article on how to use this method, but basically you supply:

  • Entity Name (in your case pricelevel)
  • ID of the record (you can get this from your lookup control)
  • Options (highly recommended to at least specify the columns you want to retrieve)
  • A function to execute when the operation succeeds
  • A function to execute if the operation fails

It should be something like this:

var pricelevelid = Xrm.Page.getAttribute("pricelevelid").getValue()[0].id;
Xrm.WebApi.retrieveRecord(
  "pricelevel", 
  pricelevelid, 
  "?$select=name,statuscode",
  function(record) { alert (record.name); },
  function(err) { alert(err); }
);
jasonscript
  • 6,039
  • 3
  • 28
  • 43