locked
oData.... Unterminated String Constant RRS feed

  • Question

  • Hi everyone.

    I am using oData to get data from a related entity and set a field on the Opportunity form required or not required based on this related entity field value.

    However, I get an error "Unterminated String Constant". I have made bold the line where it is throwing this error.

    function GetRelatedWinLoss() {
        //Getting the Lookup object from the CRM Page
        debugger;
        var winLoss = Xrm.Page.getAttribute("nn_winlossreasonidopp").getValue();
        var winLossID = "";
        // Getting the GUID for the Win Loss record
        if (winLoss != null) {
            winLossID = winLoss[0].id;
        }
        else {
            return true;
        }
    
        winLossID = winLossID.replace('{', '').replace('}', '');
    
        //Checking if we have a project GUID Value
        if (winLossID != null) {
    
            //Let’s create the Web Service URL
            oDataPath = Xrm.Page.context.getServerUrl() + "/xrmservices/2011/organizationdata.svc";
    
            //Call the Win Loss retrieve function
            RetrieveWinLossRecord(winLossID, oDataPath);
        }
    }
    
    function RetrieveWinLossRecord(Id, ODataPath) {
        var retrieveReq = new XMLHttpRequest();
        retrieveReq.open("GET", ODataPath + "/nn_winlossreasonsSet(guid'" + Id + "')", true);
        retrieveReq.setRequestHeader("Accept", "application/json");
        retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        debugger;
    
        retrieveReq.onreadystatechange = function () {
            retrieveWinLossReqCallBack(this);
        };
        retrieveReq.send();
    }
    
    function retrieveWinLossReqCallBack(retrieveWinLossReq) {
        if (retrieveWinLossReq.status == 200) {
            var retrievedWinLoss = JSON.parse(retrieveWinLossReq.responseText).d;
            var winLossName = retrievedWinLoss.nn_cancelledreason;
            if (winLossName == 1) {
                Xrm.Page.data.entity.attributes.get("nn_competitorid").setRequiredLevel("none");
            }
            else {
                Xrm.Page.data.entity.attributes.get("nn_competitorid").setRequiredLevel("required");
            }
        } else {
            alert("Error in Fetching data");
        }
    }

    I researched this error and it might have been a problem with my quotes. However, I do not see a problem with them at all.

    The strange thing is that the main functionality of the code setting the required field works.

    I just get an error message  and problems when closing the form. Anyone know where I am going wrong?

    Sunday, September 29, 2013 8:14 AM

Answers

  • Hi,

    Your code seemed fine, except for not checking the readystate first. Try the following updated code for your RetrieveWinLossRecord function. 

    function RetrieveWinLossRecord(Id, ODataPath) {
        var retrieveReq = new XMLHttpRequest();
        retrieveReq.open("GET", ODataPath + "/nn_winlossreasonsSet(guid'" + Id + "')", true);
        retrieveReq.setRequestHeader("Accept", "application/json");
        retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        debugger;
    
        retrieveReq.onreadystatechange = function () {
       	if (this.readyState == 4 /* complete */) {
        	   retrieveReq.onreadystatechange = null;
        	      if (this.status == 200) {
         		  retrieveWinLossReqCallBack(this);
        	      }
    	}
    
        };
        retrieveReq.send();
    }
    


    Eric UNG [Senior Analyst Programmer :: Sydney, Australia]

    • Marked as answer by davdatong Monday, September 30, 2013 11:41 AM
    Sunday, September 29, 2013 11:32 AM

All replies

  • Hi,

    Your code seemed fine, except for not checking the readystate first. Try the following updated code for your RetrieveWinLossRecord function. 

    function RetrieveWinLossRecord(Id, ODataPath) {
        var retrieveReq = new XMLHttpRequest();
        retrieveReq.open("GET", ODataPath + "/nn_winlossreasonsSet(guid'" + Id + "')", true);
        retrieveReq.setRequestHeader("Accept", "application/json");
        retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
        debugger;
    
        retrieveReq.onreadystatechange = function () {
       	if (this.readyState == 4 /* complete */) {
        	   retrieveReq.onreadystatechange = null;
        	      if (this.status == 200) {
         		  retrieveWinLossReqCallBack(this);
        	      }
    	}
    
        };
        retrieveReq.send();
    }
    


    Eric UNG [Senior Analyst Programmer :: Sydney, Australia]

    • Marked as answer by davdatong Monday, September 30, 2013 11:41 AM
    Sunday, September 29, 2013 11:32 AM
  • Many thanks for this. This has resolved my issue. :)
    Monday, September 30, 2013 11:42 AM