locked
JSON parse help - status of 400 (not 200) RRS feed

  • Question

  • What am I doing wrong here:

    /////////////////////////////////////////////////////////////////////////////////////////////////
    function RetrieveProductDescription(){
    
     var ExistingProduct = Xrm.Page.getAttribute('productid').getValue();
     if (ExistingProduct === null) return;
     
     var serverUrl = Xrm.Page.context.getClientUrl();
     
     var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/QuoteDetailSet(guid'" + ExistingProduct[0].id + "')?$select=description";
     
     var req = new XMLHttpRequest();
     req.open("GET", oDataSelect, false); //false = sync & true = async
     req.setRequestHeader("Accept", "application/json");
     req.setRequestHeader("Content-Type", "application/json;charset=utf-8");
     req.onreadystatechange = function () {
         if (req.readyState === 4) {
             alert(req.status);
             if (req.status === 200) { //This is where it stops. Returns a value of 400.
                 var retrieved = JSON.parse(req.responseText).d;
                 Xrm.Page.getAttribute('description').setValue(retrieved.description);
                 Xrm.Page.getAttribute('description').setSubmitMode('always');
                 }
         }
     };
     req.send();
    
    }

    Wednesday, October 30, 2013 5:15 PM

Answers

  • Revised code below works. I was cross-eyed. :)

    /////////////////////////////////////////////////////////////////////////////////////////////////
    function RetrieveProductDescription(){
    
     var ExistingProduct = Xrm.Page.getAttribute('productid').getValue();
     if (ExistingProduct === null) return;
     
     var serverUrl = Xrm.Page.context.getClientUrl();
     
     var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/ProductSet(guid'" + ExistingProduct[0].id + "')?$select=Description"; //Helps if you are looking at the correct entity (where you are looking up TO not FROM). Also, it wants the Schema name of the field on the target entity.
     
     var req = new XMLHttpRequest();
     req.open("GET", oDataSelect, false); //false = sync & true = async
     req.setRequestHeader("Accept", "application/json");
     req.setRequestHeader("Content-Type", "application/json;charset=utf-8");
     req.onreadystatechange = function () {
         if (req.readyState === 4) {
             if (req.status === 200) {         
                 var retrieved = JSON.parse(req.responseText).d;
                 Xrm.Page.getAttribute('new_productdescriptioncrm').setValue(retrieved.Description); //Again, use the Schema name.
                 Xrm.Page.getAttribute('new_productdescriptioncrm').setSubmitMode('always');
                 //ForceSave();
             }
         }
     };
     req.send();
    
    }

    • Marked as answer by M. Jaxon Wednesday, October 30, 2013 5:55 PM
    Wednesday, October 30, 2013 5:55 PM

All replies

  • Hello,

    First, you need to use "==" operator not "===". Make sure you are using correct fields to comparision, try to debug your code and see if oDataSelect URL is coming correctly or not.

    HTH


    Our Website | Our Blog | Follow US | My Facebook Page | Microsoft Dynamics CRM 2011 Application Design
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Wednesday, October 30, 2013 5:23 PM
    Moderator
  • Revised code below works. I was cross-eyed. :)

    /////////////////////////////////////////////////////////////////////////////////////////////////
    function RetrieveProductDescription(){
    
     var ExistingProduct = Xrm.Page.getAttribute('productid').getValue();
     if (ExistingProduct === null) return;
     
     var serverUrl = Xrm.Page.context.getClientUrl();
     
     var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/ProductSet(guid'" + ExistingProduct[0].id + "')?$select=Description"; //Helps if you are looking at the correct entity (where you are looking up TO not FROM). Also, it wants the Schema name of the field on the target entity.
     
     var req = new XMLHttpRequest();
     req.open("GET", oDataSelect, false); //false = sync & true = async
     req.setRequestHeader("Accept", "application/json");
     req.setRequestHeader("Content-Type", "application/json;charset=utf-8");
     req.onreadystatechange = function () {
         if (req.readyState === 4) {
             if (req.status === 200) {         
                 var retrieved = JSON.parse(req.responseText).d;
                 Xrm.Page.getAttribute('new_productdescriptioncrm').setValue(retrieved.Description); //Again, use the Schema name.
                 Xrm.Page.getAttribute('new_productdescriptioncrm').setSubmitMode('always');
                 //ForceSave();
             }
         }
     };
     req.send();
    
    }

    • Marked as answer by M. Jaxon Wednesday, October 30, 2013 5:55 PM
    Wednesday, October 30, 2013 5:55 PM