locked
CRM 2013 - What to do with FetchXML Query In Javascript RRS feed

  • Question

  • I currently need to retrieve a attribute that is present in a Sub-Grid, how I've done this previously is a C# Plugin with a FetchXML Query within to retrieve the attribute.

    However, I want to retrieve the attribute and put its context into a field on the CRM Form. Therefore I've constructed the following in a JavaScript Web Resource

    function getProduct()
    {
    var accountID = Xrm.Page.getAttribute("productid");
    	var AccountIDObj= accountID.getValue();
    		
    		if (AccountIDObj!=null)
    		{
    			AccountID= AccountIDObj[0].id;
                    }
    
    var fetch = 
    
    @<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>
      <entity name = 'quotedetail'>
        <attribute name = 'productid'/>
        <attribute name = 'productdescription'/>
        <attribute name = 'priceperunit'/>
        <attribute name = 'quantity'/>
        <attribute name = 'extendedamount'/>
        <attribute name = 'quotedetailid'/>
          <link-entity name ='product' alias='bf' to='productid' from='productid'>
            <filter type = 'and'>
              <condition attribute='productnumber' value ='" + AccountID + "' operator = 'eq'/>
            </filter>
          <link-entity name='productpricelevel' alias='bg' to='productid' from ='productid'>
            <attribute name = 'amount'/>
           </link-entity>
          </link-entity>
        </entity>
      </fetch>"
    
    

    The problem I now have is where to go from here, in C# I'd create a EntityCollection with the query and go from there, does anyone have any suggestions?

    S.Harrison

    Friday, August 22, 2014 12:11 PM

All replies

  •  Hi, Shaun,

    You can try this..

    var fetchData = XrmServiceToolkit.Soap.Fetch(fetch);
    if(fetchData == "")
    return;

    var varray = new Array;
    for (var i = 0; i < fetchData.length; i++) {
           varray=fetchData[i].attributes.productid.value;
    }

    Saturday, August 23, 2014 1:19 PM
  • Hi Suresh

    I tried your code, however I got the following error

    XrmServiceToolkit is undefined

    Any suggestions as to what I'm missing?

    Thanks, Shaun


    S.Harrison

    Tuesday, August 26, 2014 11:07 AM
  • Hi Shaun,

    XrmServiceToolkit is a js library from CodePlex. Including it as webresource simplifies your development.

    http://xrmservicetoolkit.codeplex.com/

    Regards,

    Jithesh

    Tuesday, August 26, 2014 11:52 AM
  • Hi Shaun,

    Xrmservicetoolkit is supporting js library file. you need to add it in webresource. 

    Download it from the link given by Jithesh.

    Tuesday, August 26, 2014 12:08 PM
  • Hi Both,

    I have implemented the following code;

    function getProduct()
    {
    var accountID = Xrm.Page.getAttribute("productid");
    	var AccountIDObj= accountID.getValue();
    		
    		if (AccountIDObj!=null)
    		{
    			AccountID= AccountIDObj[0].id;
                    }
    
       var fetch = "<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>" 
        fetch +="<entity name = 'quotedetail'>"
        fetch += "<attribute name = 'productid'/>";
       fetch +="<attribute name = 'productdescription'/>";
        fetch +="<attribute name = 'priceperunit'/>";
        fetch +="<attribute name = 'quantity'/>";
        fetch +="<attribute name = 'extendedamount'/>";
        fetch +="<attribute name = 'quotedetailid'/>";
        fetch +="<link-entity name ='product' alias='bf' to='productid' from='productid'>";
          fetch +="<filter type = 'and'>";
          fetch +="<condition attribute='productnumber' value ='";
    fetch += AccountID;
    fetch += "' operator = 'eq'/>";
          fetch +="</filter>";
         fetch +="<link-entity name='productpricelevel' alias='bg' to='productid' from ='productid'>";
         fetch +="<attribute name = 'amount'/>";
          fetch +="</link-entity>";
         fetch +="</link-entity>";
       fetch +="</entity>";
     fetch +="</fetch>";
    
    var fetchData = XrmServiceToolkit.Soap.Fetch(fetch);
    if(fetchData!=null)
    {
        var varray = new Array;
        for(var i=0; i <fetchData.length; i++)
        {
            varray=fetchData[i].attributes.amount.value;
        }
    alert(varray);
    }
    }
    


    I have the issue where when I'm calling an alertbox to see what's in varray it's blank, I have changed

    attributes.productid

    to

    attributes.amount

    as that was the attribute I want to retrieve, however even when it was productidthe alert box was still null

    Any suggestions?


    S.Harrison

    Wednesday, August 27, 2014 8:17 AM
  • Hello Shawn,

    use xrm toolbox or some tool to find if your fetch is returning any result in your organization. Also try to use F12 ie debugger to explore the result of fetch by adding breakpoint.

    Also please modify Array to Array().

    regards,

    Jithesh

    Wednesday, August 27, 2014 11:47 AM
  • Hi Shaun,

    I havent tried with linked entity..

    First go as Jithesh suggested and then try by below line or link.

    //To get attribute values from link entity use alias name like

    fetchData[i].attributes.bg.amount.value; 

    Below link will help to get over from your problem.

    http://rajeevpentyala.wordpress.com/2013/06/20/retrieve-records-with-fetchxml-using-jscript-crm-2011/


    • Edited by Suresh Sorde Wednesday, August 27, 2014 12:06 PM
    Wednesday, August 27, 2014 11:51 AM
  • Hi Again Both

    I implemented both your recommendations of Array() and attributes.bg.amount.value;

    However still no luck

    I then took Jithesh's advice of adding a debugger, below is a screenshot

    However my knowledge of debugging javascript is limited

    Have either of you got any suggestions?


    S.Harrison

    Wednesday, August 27, 2014 1:00 PM
  • please press F10 to step into next statement and f11 to step inside a function. It is std JavaScript function.

    once you pass the variable, right click and add watch or inspect the variable.

    google for further information.

    regards,

    Jithesh

    Wednesday, August 27, 2014 1:36 PM
  • Hi again,

    While debugging I noticed that when the for(var i=0; i <fetchData.length; i++)

    After i is set to 0 it is then going to i < fetchData.length, but then jumping straight to the alert, therefore it is not completing the loop to retrieve the actual data required


    S.Harrison



    • Edited by Shaun Harrison Wednesday, August 27, 2014 2:50 PM added image
    Wednesday, August 27, 2014 2:43 PM
  • friend,

    inspect what is inside fetchData variable. That should hold the result of the fetch query execution. It is possible that the fetch is returning 0 records.

    regards,

    Jithesh

    Wednesday, August 27, 2014 9:14 PM
  • Hello There,

    You can try this 

    varray=fetchData[i].attributes["bg.amount"].value instead varray=fetchData[i].attributes.bg.amount.value

    this way of representation worked for me.


    Lakshmi Dudala

    Wednesday, March 30, 2016 7:05 AM