locked
object object RRS feed

  • Question

  • I am back on the coding trail and struggling to get this javascript working:

    function QuotePotentials(QuoteName)
    {
        var serverUrl = document.location.protocol + "//" + document.location.host + "/" + Xrm.Page.context.getOrgUniqueName();
        var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/";
        oDataEndpointUrl += "QuoteSet?$select=QuoteNumber,quote_details/sp_SellingProductId&$expand=quote_details&$filter=QuoteNumber eq '" + QuoteName + "'";
    
       //    oDataEndpointUrl += "QuoteSet?$select=quote_details/sp_SellingProductId&$expand=quote_details&$filter=QuoteNumber eq 'QUO-02084-M3Z0'";
    
        var service = GetRequestObject();
    
        if (service != null)
        {
            service.open("GET", oDataEndpointUrl, false);
            service.setRequestHeader("X-Requested-Width", "XMLHttpRequest");
            service.setRequestHeader("Accept", "application/json, text/javascript, */*");
            service.send(null);
            var requestResults = eval('(' + service.responseText + ')').d;
    
            alert(requestResults.results.length);
    
            if (requestResults != null && requestResults.results.length > 0)
            {
                var QuotePots = "";
                for (var i = 0; i < requestResults.results.length; i++)
                {
                    alert(requestResults.results[i]);
                    var res = requestResults.results[i];
                    var resId = res.sp_SellingProductId;
                    alert(resId);
    
    
    
    
                    QuotePots = QuoteProdGroup(requestResults.results[i].sp_SellingProductId);                
                }
                return QuotePots;
            }
        }
        return false;
     }
    

    The first alert tells me the number of lines returned from the quote.

    The second alert says [object Object].

    The third alert says undefined.

    Below is the Results (JSON) I get from using the OData Query Designer when I run it.

    {
    "d" : {
    "results": [
    {
    "__metadata": {
    "uri": "http://aserver/XRMServices/2011/OrganizationData.svc/QuoteSet(guid'4c9e3192-eaf6-e311-ba1a-02bf0a86f1e1')", "type": "Microsoft.Crm.Sdk.Data.Services.Quote"
    }, "quote_details": {
    "results": [
    {
    "__metadata": {
    "uri": "http://aserver/XRMServices/2011/OrganizationData.svc/QuoteDetailSet(guid'3a1ea403-a268-4f93-94e9-49f89a10e600')", "type": "Microsoft.Crm.Sdk.Data.Services.QuoteDetail"
    }, "sp_SellingProductId": {
    "__metadata": {
    "type": "Microsoft.Crm.Sdk.Data.Services.EntityReference"
    }, "Id": "1ac1e0d9-9d8c-e211-a049-02bf0a86f1e1", "LogicalName": "product", "Name": "PPS200 c/w SLV"
    }
    }
    ]
    }
    }
    ]
    }
    }

    I am trying to pull put the GUID for the product and then pass this to another function, but can't reference it.

    Any help would be appreciated.

    Matt

    Thursday, June 19, 2014 1:23 PM

Answers

All replies

  • you are missing the inner "results"

    for (var i = 0; i < requestResults.results.length; i++) // here you are fetching the first results
    {
        var res = requestResults.results[i].quote_details;
        for (var j = 0; j < res.results.length; j++) // here we fetch the second results
        {
            var resId = res.results[j].sp_SellingProductId.Id;
            alert(resId);
        }
    }


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



    Thursday, June 19, 2014 2:06 PM
  • I have updated it to:

     if (requestResults != null && requestResults.results.length > 0)
            {
                var QuotePots = "";
                alert(requestResults.results.length);
                for (var i = 0; i < requestResults.results.length; i++) // fetch the first results
                {
                    var res = requestResults.results[i];
                    alert(res.results.length);
                    for (var j = 0; j < res.results.length; j++) // fetch the second results
                    {
                       var resId = res.results[j].sp_SellingProductId;
                       alert(resId);
                    }
                }
    

    I now receive the following error:

    Microsoft Dynamics CRM Error Report Contents
    
    <CrmScriptErrorReport>
      <ReportVersion>1.0</ReportVersion>
      <ScriptErrorDetails>
       <Message>Unable to get property 'length' of undefined or null reference</Message>
       <Line>420</Line>
       <URL>/main.aspx?etc=1084&extraqs=%3f_gridType%3d1084%26etc%3d1084%26id%3d%257b4C9E3192-EAF6-E311-BA1A-02BF0A86F1E1%257d%26pagemode%3diframe%26preloadcache%3d1403189915651%26rskey%3d583749768&pagetype=entityrecord</URL>
       <PageURL>/main.aspx?etc=1084&extraqs=%3f_gridType%3d1084%26etc%3d1084%26id%3d%257b4C9E3192-EAF6-E311-BA1A-02BF0A86F1E1%257d%26pagemode%3diframe%26preloadcache%3d1403189915651%26rskey%3d583749768&pagetype=entityrecord</PageURL>
       <Function></Function>
       <CallStack>
       </CallStack>
      </ScriptErrorDetails>
      <ClientInformation>
       <BrowserUserAgent>Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)</BrowserUserAgent>
       <BrowserLanguage>en-US</BrowserLanguage>
       <SystemLanguage>en-US</SystemLanguage>
       <UserLanguage>en-GB</UserLanguage>
       <ScreenResolution>1920x1080</ScreenResolution>
       <ClientName>Web</ClientName>
       <ClientTime>2014-06-19T15:58:41</ClientTime>
      </ClientInformation>
      <ServerInformation>
        <OrgLanguage>1033</OrgLanguage>
        <OrgCulture>2057</OrgCulture>
        <UserLanguage>1033</UserLanguage>
        <UserCulture>2057</UserCulture>
        <OrgID>{243A0FF8-F595-E211-96EA-02BF0A86F1E1}</OrgID>
        <UserID>{82BC61EF-740C-E211-A4D0-02BF0A86F1E1}</UserID>
        <CRMVersion>5.0.9690.2865</CRMVersion>
      </ServerInformation>
    </CrmScriptErrorReport>

    Any ideas?  I assume it is saying there is no data, but the first result says there is data???

    Matt

    Thursday, June 19, 2014 3:02 PM
  • I forgot to add quote_details, I updated my previous code, line

    var res = requestResults.results[i].quote_details;

    and

    var resId = res.results[j].sp_SellingProductId.Id;

    try now


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


    Thursday, June 19, 2014 3:13 PM
  • Thanks for this, now have:

    if (requestResults != null && requestResults.results.length > 0)
            {
                var QuotePots = "";
                for (var i = 0; i < requestResults.results.length; i++) // fetch the first results
                {
                    var res = requestResults.results[i].quote_details;
                    for (var j = 0; j < res.results.length; j++) // fetch the second results
                    {
                       var resId = res.results[j].sp_SellingProductId;
                       alert(resId);
                    }
                }
    

    But the alert just shows object object.

    Matt

    Thursday, June 19, 2014 3:23 PM
  • I modified also the resId line, you need to add .Id at the end

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

    • Marked as answer by Matt_Hirst_UK Thursday, June 19, 2014 3:31 PM
    Thursday, June 19, 2014 3:28 PM
  • Thanks, that now works as expected!

    Is there a better way to write javascript code other than direct into CRM, somewhere you can debug it, etc, etc?

    regards,

    Matt

    Thursday, June 19, 2014 3:31 PM
  • You can used the debugger included inside Internet Explorer, check this link: http://msdynamicscrmblog.wordpress.com/2013/12/20/dynamics-crm-2011-javascript-debugging-in-internet-explorer-11/

    or google: debug javascript dynamics crm, there are several links depeding also on which rollup you are.


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

    Thursday, June 19, 2014 3:37 PM
  • Hi,

    Switch on debugging on your browser.

    Add the word "debugger;" and your browser should bring up tool options to debug.

    Much easier than alert.

    Another thought on your problem. You could cast the "resID" to an "EntityReference" type.

    Don


    Thursday, June 26, 2014 9:08 PM