locked
CRM2013 Retrived Opportunity records Error Message RRS feed

  • Question

  • HI

    The following JavaScript i apply into my CRM2013, to retrieve Opportunity Record. but its not working

    function GetOpportunity(lotid) {
    alert("Start GetOpportunity");
        var serverUrl = location.protocol + "//" + location.host + "/" + Xrm.Page.context.getOrgUniqueName();
        var oDataUri = serverUrl + "/xrmservices/2011/OrganizationData.svc/OpportunitySet?$select=statecode&$filter=ppc_lotid/Id eq guid'" + lotid + "'";
        var jSonArray = new Array();
    
        jQuery.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: oDataUri,
            async: false,
            beforeSend: function (XMLHttpRequest) {
                //Specifying this header ensures that the results will be returned as JSON.            
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
            },
            success: function (data, textStatus, XmlHttpRequest) {
                if (data && data.d != null) {
                    jSonArray.push(data.d);
                }
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) {
                alert("Error :  has occured during retrieval of the opportunity");
            }
        });
    
    	for (var i = 0; i < jSonArray.length; i++)
    	{
    		if (jSonArray[i] == 'Won') 
    		{
    			return true;
    		}
    	}	
        return false;
    alert("End GetOpportunity");	
    }

    Its totally not working and show me the following error message.

    what can I change from the javascript ?

    Thank you!

    Wednesday, November 19, 2014 9:43 AM

All replies

  • Can you check whether your oData query is correct?  You can download the Dynamics CRM oData Query designer from here (https://crm2011odatatool.codeplex.com/) and build your query and test.

    You can give an existing ppc_lotid and test your query.

    Thanks


    Sachith Chandrasiri

    Sunday, November 30, 2014 2:59 AM
  • Hi,

    Check your OData query. you can try using below code

    var retrieveRecordsReq = new XMLHttpRequest();
    var ODataPath = Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc/OpportunitySet?$select=statecode&$filter=ppc_lotid/Id eq guid'" + lotid + "'";
     retrieveRecordsReq.open('GET', ODataPath2, false);
     retrieveRecordsReq.setRequestHeader("Accept", "application/json");
     retrieveRecordsReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
     retrieveRecordsReq.send(null);
        var records2 = JSON.parse(retrieveRecordsReq.responseText).d;
        var count2 = records2.results.length
         if (records2.results.length > 0) {
            var Opportunity= records2.results[0].opportunityname;
        }

    Thanks,

    Prasad

    Monday, December 1, 2014 4:35 AM
  • Hi,

    You can follow the code given by Prasad or if you have any other requirement, you can use OData Query Designer for generating OData query and use the same thing in JavaScript.

    https://crm2011odatatool.codeplex.com/

    --

    Regards,

    Gopinath.

    • Proposed as answer by Gopinath Reddy Monday, December 1, 2014 8:48 AM
    Monday, December 1, 2014 8:48 AM