Aggregate FetchXML Queries in CRM 2011/2013/2015


Today I got a requirement to calculate the sum of the invoice records in CRM.

Normally, we can do retrieve all records from CRM and loop through each record and calculate the sum of the total amount.

Here is the way to do using Fetch XML where looping is not required.

string strFetchXML = @”
EntityCollection entInvColl = iService.RetrieveMultiple(new FetchExpression(strFetchXML));
Entity entInvoice = entInvColl.Entities[0];
AliasedValue avTotalAmount = (AliasedValue)entInvoice.Attributes[“inv.totalamount”];
Money mnyValue = (Money)(avTotalAmount.Value);
decimal decValue = mnyValue.Value;

Hope this helps

Happy CRM’ing


One thought on “Aggregate FetchXML Queries in CRM 2011/2013/2015

  1. Unknown March 6, 2017 / 9:05 pm

    Hi Gopi, where you added all invoice total amount in your code. Can you please highlighted it?Thanks,Andy


Leave a Reply

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

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

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s