Tuesday 31 May 2011

How to Group using FetchXML

In Microsoft Dynamics CRM 2011 FetchXML includes grouping and aggregation features which let us to use aggregate functions like sum, count etc.

Using Group by we can calculate sum, avg, min, max, count. But, Group by clause is not supported through LINQ in CRM.

You can only specify one aggregate attribute in a query and you cannot use the distinct keyword. To create an aggregate attribute, set the keyword aggregate to true, then specify valid entity name, attribute name and alias(variable name). You must also specify the type of aggregation you want to perform.

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

Below is the example to get sum of total amount of all won quotes:

string quotes = @"
<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='quote'>
<attribute name='totalamount' alias='totalamount_sum' aggregate='sum'/>
<attribute name='statecode' groupby='true' alias='state' />
<filter type='and'>
<condition attribute=' statecode ' operator='eq' value='won' />"+
"</filter> "+
"</entity> "+
"</fetch>";

EntityCollection quotes_result = _service.RetrieveMultiple(new FetchExpression(quotes));
foreach (var q in quotes_result.Entities)
{
Decimal wonQuoteAmount = ((Money)((AliasedValue)q["totalamount_sum"]).Value).Value;
}

courtesy of:
http://community.dynamics.com/product/crm/crmtechnical/b/crminogic/archive/2011/04/23/group-by-using-fetchxml.aspx

Thursday 19 May 2011

CRM2011 FetchXML Row Count

How to find the row count in CRM2011 using fetchxml, just add returntotalrecordcount='true' into the first tag as per below.
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" returntotalrecordcount='true'>

enjoy!

Wednesday 11 May 2011

Top 10/x Records

Bringing back the top 10 records in CRM2011 using fetchxml is extremly simple, you just need to add in count="10" into the first fetch tag as per below

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true" count="10">
  <entity name="bc_renewal">
    <attribute name="subject" />
  </entity>
</fetch>

Monday 9 May 2011

Fetchxml CRM 2011 Pre-Filtering

How to turn CRM 2011 Pre-filtering on for Reporting Services Reports.

Open the dataset, edit the fetchxml query and add in the enableprefiltering and prefilterparametername box, this description can be whatever you like.

<fetch  version="1.0"
            output-format="xml-platform"
           mapping="logical"
           distinct="false">


<entity name="opportunityproduct"
                       enableprefiltering="true"
                       prefilterparametername="OpportunityProdFilter">

ADFS Timeout on Server 2012 for CRM 2011

This follows on from this Microsoft Guide http://social.technet.microsoft.com/wiki/contents/articles/7681.setting-the-adfs-timeout-for-crm...