Aggregate FetchXml Queries for Dynamics CRM 2011/2013

Recently needed to figure out ways to find sum of various child entity attribute under a parent entity and came across the following way in FetchXml using which can avoid looping through all child entity collection to do various operations such as:

  • sum
  • avg
  • min
  • max
  • count(*)
  • count(attribute name)

Sample FetchXml for sum:

"<fetch distinct='false' mapping='logical' aggregate='true'> <entity name='entity name'> <attribute name='attribute name' aggregate='sum' alias='alias name'/> </entity> </fetch>"

Result xml will be as follows:

"<resultset morerecords="0"'> 
   <result>
      <alias>aggregate value</alias>
   </result>
</resultset>"

Hope it helps!

Advertisement

15 thoughts on “Aggregate FetchXml Queries for Dynamics CRM 2011/2013

  1. Hi deepesh,

    This one looks like the solution of solutions!
    But i didn’t get it, result aggregate value, that means that i have to put sum in the alias tags? :S Sorry for the question 😦

      • Deepesh , sorry 😦

        But i don’t get it… check my script :/

        function sumTotals(){

        var relatedTC = document.getElementById(“Gastos_TC”);

        var lookupfield = [];

        lookupfield = Xrm.Page.getAttribute(“transactioncurrencyid”).getValue();

        if (lookupfield != null) {
        var lookupid = lookupfield[0].id;
        } else {
        return;
        }

        var fetchXml = “”
        + “”
        + “”
        + “”
        + “”
        fetchXml += “”;
        fetchXml += “”;
        // uiname=’Peso’ uitype=’transactioncurrency’
        fetchXml += “”;
        fetchXml += “”;
        fetchXml += “”
        fetchXml += “”;
        fetchXml += “”;
        fetchXml += “”;

        var Result = “”
        +””
        +”sum”
        +””
        Result += “”;

        alert(Result);
        }

  2. Sorry… this is the script
    function sumTotals(){
    // Get a grid
    var relatedTC = document.getElementById(”Gastos_TC”);
    // compare the currency field from parent and child (the child displays in the grid)
    var lookupfield = [];

    lookupfield = Xrm.Page.getAttribute(”transactioncurrencyid”).getValue();

    if (lookupfield != null) {
    var lookupid = lookupfield[0].id;
    } else {
    return;
    }
    //get the data from the child to be displayed in the parent form grid
    var fetchXml = ””
    + ””
    + ””
    + ””
    + ””
    fetchXml += ””;
    fetchXml += ””;
    // uiname=’Peso’ uitype=’transactioncurrency’
    fetchXml += ””;
    fetchXml += ””;
    fetchXml += ””
    fetchXml += ””;
    fetchXml += ””;
    fetchXml += ””;

    var Result = ””
    +””
    +”sum”
    +””
    Result += ””;

    alert(Result);
    }

  3. Gooood, The Script does not show :/

    function sumTotals(){

    var parent = [];

    parent = Xrm.Page.getAttribute("new_reembolsodegastoid").getValue();

    if (parent != null) {
    var parentid = parent[0].id;
    } else {
    return;
    }

    var fetchXml = ""
    + ""
    + ""
    + ""
    + ""
    fetchXml += "";
    fetchXml += "";
    // uiname='Peso' uitype='transactioncurrency'
    fetchXml += "";
    fetchXml += "";
    fetchXml += ""
    fetchXml += "";
    fetchXml += "";
    fetchXml += "";

    var Result = ""
    +""
    +"sum"
    +""
    Result += "";

    alert(Result);
    }

    into the code Tags to see >.<'

  4. And again >.< Sorry, sorry…

    function sumTotals(){
    // Get a grid
    var relatedTC = document.getElementById( Gastos_TC );
    // compare the currency field from parent and child (the child displays in the grid)
    var lookupfield = [];

    lookupfield = Xrm.Page.getAttribute( transactioncurrencyid ).getValue();

    if (lookupfield != null) {
    var lookupid = lookupfield[0].id;
    } else {
    return;
    }
    //get the data from the child to be displayed in the parent form grid
    //var fetchXml =
    //+
    //+
    //+
    //+
    //fetchXml += ;
    //fetchXml += ;
    // uiname='Peso' uitype='transactioncurrency'
    //fetchXml += ;
    //fetchXml += ;
    //fetchXml +=
    //fetchXml += ;
    //fetchXml += ;
    //fetchXml += ;

    //var Result =
    // +
    // + sum
    // +
    //Result += ;

    alert(Result);
    }

  5. Hi Deepesh!

    You’re Right, mi first fetch was incorrect, but now i’m fix it but still does not Works… i explain you what i’m trying to do.

    With a JavaScript i’m getting the values of the child entity and sum them with for every specific type of additional cost that i have registered. I have 4 types of them, so in the parent entity i show every cost in a specific grid.

    But now i need to get the total value doing the sum of every record cost type.

    This is one of the Scripts with one i’m trying to do that sum taking you example


    var fetchXml = ""
    + ""
    + ""
    + ""
    fetchXml += "";
    fetchXml += "";
    fetchXml += "";
    fetchXml += "";
    fetchXml += "";

    ""
    ""
    ""
    ""
    "";

    Thank you for any help that you could give me!

    Regards.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s