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!
[…] Aggregate FetchXml Queries for Dynamics CRM 2011/2013 […]
[…] Aggregate FetchXml Queries for Dynamics CRM 2011/2013 […]
I am trying following but it is not working
Hi Mak,
I tried the following fetchxml on CRM online trial:
and it seemed to work on vanilla crm version. Can you check once and do let me know if you face issues.
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 😦
Aggregate Value means the output. 🙂
Hope it helps, if you run it once it will be clear!
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);
}
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);
}
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 >.<'
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);
}
Hi ,
This fetch is incorrect. Please formulate a correct one like following sample:
http://sliong.wordpress.com/2012/11/14/crm-2011-get-record-count-with-fetchxml/
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.
What I will do is give a sample in the same blog after editing it, so that you will understand better. I am afraiud that the above fetchxml is still wrong.
[…] https://dynamicsofdynamicscrm.wordpress.com/2014/07/23/aggregate-fetchxml-queries-for-dynamics-crm-2… […]
[…] https://dynamicsofdynamicscrm.com/2014/07/23/aggregate-fetchxml-queries-for-dynamics-crm-20112013/ […]