locked
Odata not working when trying to fetch single field value from AccountSet from opportunity page RRS feed

  • Question

  • Hi ,

    I am trying to fetch the value of an custom field (accountcategorycode) from Account. I am using Odata to retrieve the field value from the Account to which the Opportunity belongs. But i am continually getting Syntax error or Access denied . Please help. PFB the code..Let me know what is wrong.

    function getAccount()
    {
        var lookupAccount = Xrm.Page.getAttribute("customerid").getValue();
        if ((lookupAccount != null))
        {


            var lookupid = lookupAccount[0].id;
       }
           // alert(lookupid);

    //lookupid = lookupid.replace('{', '').replace('}', '');

        // Getting the REST End point service URL
    var serverUrl = document.location.protocol + "//" +  document.location.host+ "/" + Xrm.Page.context.getOrgUniqueName();

        var url = serverUrl  + "/XRMServices/2011/OrganizationData.svc/";
    var odataSelect = url + "AccountSet?$select= accountcategorycode&$filter=AccountId" + " eq guid'" + lookupid + "'";

        //var url = document.location.protocol + "//" + document.location.host + "/xrmservices/2011/OrganizationData.svc/";
        //url += oDataQueryUrl;

        // Get Windows XMLHTTP service request object
        var service = GetRequestObject();

        if (service != null) {
           // try {
                service.open("GET",odataSelect, false);
                service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
                // Set the header prperly to get JSON response
                service.setRequestHeader("Accept", "application/json, text/javascript, */*");
                service.send(null);
                if (service.responseText != null && service.responseText != "") {

                    // Get the result from service
                    var requestResults = eval('(' + service.responseText + ')').d;

      alert("Success");
                    //if (requestResults != null && requestResults.results != null && requestResults.results.length >= 1) {
                        //return requestResults.results;
                   // }
                }
           // } catch (e)
    //{
      //      }
      //  }
        //return null;
    }
    };

     

    Thursday, June 12, 2014 10:07 AM

All replies

  • Hi,

    Cleaned up your code, made some fixes, try this out:

    function getAccount()
    {
    	var lookupAccount = Xrm.Page.getAttribute("customerid").getValue();
    	
    	if (lookupAccount != null)
    	{
    		var lookupid = lookupAccount[0].id;
    		
    		// Getting the REST End point service URL
    		var serverUrl = document.location.protocol + "//" +  document.location.host+ "/" + Xrm.Page.context.getOrgUniqueName();
    
    		var url = serverUrl  + "/XRMServices/2011/OrganizationData.svc/";
    		var odataSelect = url + "AccountSet?$select= AccountCategoryCode&$filter=AccountId eq guid'" + lookupid + "'";
    
    		// Get Windows XMLHTTP service request object
    		var service = GetRequestObject();
    
    		if (service != null) 
    		{
    			try 
    			{
    				service.open("GET",odataSelect, false);
    				service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
    				// Set the header prperly to get JSON response
    				service.setRequestHeader("Accept", "application/json, text/javascript, */*");
    				service.send(null);
    				if (service.responseText != null && service.responseText != "") 
    				{
    					// Get the result from service
    					var requestResults = eval('(' + service.responseText + ')').d;
    					alert("Success");
    				}
    			}
    			catch (e) 
    			{
    
    			}
    		}
    	}
    };


    Admin QuikView Solution for CRM 2013

    Thursday, June 12, 2014 10:54 AM
  • Hi,

    I tried out the script on Form Load event. Although there is no error thrown the alert for Success is not showing. Does this mean that on success of the JSON response no data is passed?.

    Also in the Solution that i am working in, alreday has JSON and Jquery 1.7.1 scripts are alreday present. Am i missing anything on this.??

    Thursday, June 12, 2014 11:44 AM
  • Hi,

    Did you try debugging the script and checking what might have gone wrong?


    Admin QuikView Solution for CRM 2013

    Thursday, June 12, 2014 11:46 AM
  • Hi

    Tried using debugging, no issue was found.

    When i tried to alert(service.responseText) found that its giving error 404 File not found error .

    also the field name which i am trying to get from Account, its schema name is accountcategorycode...Do we need to put the same schema name of the field in the Odata query.?

    Friday, June 13, 2014 6:41 AM
  • Try with this code:

    function getAccount() {
        var lookupAccount = Xrm.Page.getAttribute("customerid").getValue();
        if (lookupAccount != null) {
            var accountId = lookupAccount[0].id;
            var serverUrl;
            if (Xrm.Page.context.getClientUrl !== undefined) {
                serverUrl = Xrm.Page.context.getClientUrl();
            } else {
                serverUrl = Xrm.Page.context.getServerUrl();
            }
            // build the request
            var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc"; 
            var accountRequest = new XMLHttpRequest();
            accountRequest.open("GET", ODataPath + "/AccountSet(guid'" + accountId + "')", false); 
            accountRequest.setRequestHeader("Accept", "application/json"); 
            accountRequest.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            // execute the request
            accountRequest.send();
            if (accountRequest.status === 200) {
                var retrievedAccount = JSON.parse(accountRequest.responseText).d;
                // retrieve the value of Account Category Code (it's optionset field)
                var accountcode = retrievedAccount.AccountCategoryCode.Value;
    			alert(accountcode);
            }
            else {
                alert("error");
            }
        }
    }


    My blog: www.crmanswers.net - Rockstar 365 Profile


    Friday, June 13, 2014 8:11 AM
  • Tried with above code.. But it seems its going directly to the else block and giving me the error alert.

    Also tweaked the code in the If portion

     var retrievedAccount = JSON.parse(accountRequest.responseText).d;
                // retrieve the value of Account Category Code (it's optionset field)
                       var acctype= retrievedAccount.results[0].accountcategorycode;
                            alert(acctype);

     but this also seems to give the same issue.


    Santanu....

    Friday, June 13, 2014 9:32 AM