Search code examples
dynamics-crmcrmdynamics-crm-2011fetchxml

Using FetchXML in CRM 2011


I am using FetchXML and I am grouping and using count for two of the entities but the rest of the entities I don't need grouped I just need the data to be pulled down. For example this is my code:

string groupby1 = @" 
<fetch distinct='false' mapping='logical' aggregate='true'> 
 <entity name='opportunity'>
  <attribute name='name' alias='opportunity_count' aggregate='countcolumn' />  
  <attribute name='ownerid' alias='ownerid' groupby='true' />
  <attribute name='createdon' alias='createdon' /> 
  <attribute name='customerid' alias='customerid' /> 
 </entity> 
</fetch>";

EntityCollection groupby1_result = orgProxy.RetrieveMultiple(new FetchExpression(groupby1));

foreach (var c in groupby1_result.Entities)
{
  Int32 count = (Int32)((AliasedValue)c["opportunity_count"]).Value;
  string OwnerID = ((EntityReference)((AliasedValue)c["ownerid"]).Value).Name;
  DateTime dtCreatedOn = ((DateTime)((AliasedValue)c["createdon"]).Value);
  string CustomerName = ((EntityReference)((AliasedValue)c["customerid"]).Value).Name;
}

But I get this error:

EXCEPTION: System.ServiceModel.FaultException`1[Microsoft.Xrm.Sdk.OrganizationServiceFault]: An attribute can not be requested when an aggregate operation has been specified and its neither groupby nor aggregate. NodeXml : (Fault Detail is equal to Microsoft.Xrm.Sdk.OrganizationServiceFault).

How do you use aggregates on some values and not others?


Solution

  • The error message is giving you the answer - this isn't possible with FetchXML. You'll need to make two FetchXML calls; one for your aggregates and one for your data.