locked
Can't get past status == 200 on oData request RRS feed

  • Question

  • I'm trying to write a jQuery query for CRM 2013. It is called from the onLoad of the Enquiry form - I need to get all Customer Quotes with that Enquiry's id. I'm a totaly beginner when it comes to oData, so the code below is from examples I've found online. I can't get past the if statement that checks for status == 200. I only see the alerts up as far as "Here 2".

    My oDataPath is fine - I put it into a browser and I got xml back, which looks fine, so I presume it could be something with my filter? I want to get back the vendor Id column from the Customer Quote entity, where the Enquiry id on the Customer Quote equals the id of the current Enquiry record I'm looking at. There's a 1:M relationship between Enquiry and Customer Quote.

    I'd appreciate any help with this! Remember, I'm not very knowledgeable in this area!

    Thanks.

    function QueryCustomerQuotes () {
    
    alert("In QueryCustomerQuotes");
    
    	var enquiryId =Xrm.Page.data.entity.getId();
    	enquiryId = enquiryId .replace('{', '').replace('}', '');
    alert("enquiryId: " + enquiryId);
    	
    	var oDataPath = Xrm.Page.context.getServerUrl() + "/xrmservices/2011/organizationdata.svc";
    alert("oDataPath: " + oDataPath);
                    var filter = "/new_customerquoteSet?" +
                                    "$select=new_vendorid" + 
                                    "&$filter=new_enquiryid/Id eq (guid'" + enquiryId + "')";
    
    
    alert("filter: " + filter);
    	var retrieveRecordsReq = new XMLHttpRequest();
    	retrieveRecordsReq.open("GET", oDataPath + filter, true);
    	retrieveRecordsReq.setRequestHeader("Accept", "application/json");
    	retrieveRecordsReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    	retrieveRecordsReq.onreadystatechange = function () {
    	alert("Here 1");
    		if (this.readyState == 4) {
    		alert("Here 2");
    		   if (this.status == 200) {
    		   alert("Here 3");
    			   var retrievedRecords = JSON.parse(retrieveRecordsReq.responseText).d;
    			   if(retrievedRecords.results.length > 0)
    			   {
    				   var vendorIds = retrievedRecords.results;
    alert("vendorIds[0]: " + vendorIds[0]);				   
    			   }
    		   }
    		}
    	};
    	retrieveRecordsReq.send();  
    }


    Wednesday, April 9, 2014 4:21 PM

All replies

  • Can you pattern your request off this? It gets the primary contact from an account - it tries to mimic what your are doing.

    Double check the schema names on the fields and also your filter on the Id doesn't need a "/Id"

    var req = new XMLHttpRequest();
    req.open('GET', encodeURI(Xrm.Page.context.getClientUrl() + "/XRMServices/2011/OrganizationData.svc/AccountSet?$select=PrimaryContactId&$filter=AccountId eq guid'64791BDB-A0AE-E311-8A9D-6C3BE5A8CA94'"), true);
    req.setRequestHeader('Accept', 'application/json');
    req.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var returned = JSON.parse(req.responseText).d;
                var results = returned.results;
                var PrimaryContactId = results[0].PrimaryContactId;
                alert(PrimaryContactId.Name);
            }
            else {
                alert(this.statusText);
            }
        }
    };
    req.send();


    Jason Lattimer
    My Blog -  Follow me on Twitter -  LinkedIn

    Wednesday, April 9, 2014 5:26 PM
    Moderator
  • Thank you for your reply, with a few tweaks, that gave me almost everything I need!

    Here is the response I get, without the filter:

    - <entry>
    
    
    
      <id>https://cam0.crm4.dynamics.com/XRMServices/2011/OrganizationData.svc/new_customerquoteSet(guid'ea997171-a0c7-e211-8933-3c4a92dbd8ab')</id> 
    
    
      <title type="text">00000098 Company Ltd</title> 
    
    
      <updated>2014-04-10T08:31:03Z</updated> 
    
    
    - <author>
    
    
    
      <name  /> 
    
      </author>
    
    
      <link  rel="edit" title="new_customerquote" href="new_customerquoteSet(guid'ea997171-a0c7-e211-8933-3c4a92dbd8ab')" /> 
    
    
      <category  term="Microsoft.Crm.Sdk.Data.Services.new_customerquote" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> 
    
    
    - <content type="application/xml">
    
    
    
    - <m:properties>
    
    
    
    - <d:new_EnquiryId m:type="Microsoft.Crm.Sdk.Data.Services.EntityReference">
    
    
    
      <d:Id m:type="Edm.Guid">8f532f1d-9ec7-e211-8933-3c4a92dbd8ab</d:Id> 
    
    
      <d:LogicalName>new_enquiry</d:LogicalName> 
    
    
      <d:Name>00000098</d:Name> 
    
      </d:new_EnquiryId>
    
    
    - <d:new_VendorId m:type="Microsoft.Crm.Sdk.Data.Services.EntityReference">
    
    
    
      <d:Id m:type="Edm.Guid">eb09ed20-9fc7-e211-8933-3c4a92dbd8ab</d:Id> 
    
    
      <d:LogicalName>new_supplierquoterequestsupplier</d:LogicalName> 
    
    
      <d:Name>BASF SE</d:Name> 
    
      </d:new_VendorId>
    
      </m:properties>
    
      </content>
    
      </entry>

    I need to get the Id value of new_VendorId, but I don't know how to do this.

    Here's the URL for the page:

    https://cam0.crm4.dynamics.com/xrmservices/2011/organizationdata.svc/new_customerquoteSet?$select=new_VendorId,new_EnquiryId

    I've tried various variations of this, such as appending  &$filter=new_EnquiryId.Id%20eq%20guid'C6F2A5F0-A6A3-E311-8729-6C3BE5BE8D8C', but I can't get the Id value from within new_VendorId. Does anyone know how I can get this?

    Thanks.


    Thursday, April 10, 2014 8:43 AM