I am consistently getting the above error when attempting to get results from a FetchXML query. Any help would be appreciated. The error always occurs on the line:
EntityCollection GetAllOpenActivitiesXML_result = service.RetrieveMultiple(fetched1);
Here is the rest of the code:
ITracingService tracingService = executionContext.GetExtension<ITracingService>();
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
Entity opp = new Entity("opportunity");
opp.Id = context.PrimaryEntityId;
string GetAllOpenActivitiesXML = @"
<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='activitypointer'>
<attribute name='activityid' aggregate='count' alias='activityid'/>
<filter type='and'>
<condition attribute='statecode' operator='eq' value='0' />
</filter>
<link-entity name='opportunity' from='opportunityid' to='regardingobjectid'>
<filter type='and'>
<condition attribute='name' operator='eq' value='MyOpportunity' />
</filter>
</link-entity>
</entity>
</fetch>";
try {
FetchExpression fetched1 = new FetchExpression(GetAllOpenActivitiesXML);
EntityCollection GetAllOpenActivitiesXML_result = service.RetrieveMultiple(fetched1);
foreach (var c in GetAllOpenActivitiesXML_result.Entities)
{
Int32 totalOpenActivities = (Int32)((AliasedValue)c["activityid"]).Value;
opp["new_openactivities"] = totalOpenActivities;
}
}
catch {
opp["new_openactivities"] = 0;
}
service.Update(opp);
The issue is caused because you're using the name of the field as alias so if you change the alias it will start working:
<fetch distinct="false" mapping="logical" aggregate="true" >
<entity name="activitypointer" >
<attribute name="activityid" alias="activityid_count" aggregate="count" />
<filter type="and" >
<condition attribute="statecode" operator="eq" value="0" />
</filter>
<link-entity name="opportunity" from="opportunityid" to="regardingobjectid" >
<filter type="and" >
<condition attribute="name" operator="eq" value="MyOpportunity" />
</filter>
</link-entity>
</entity>
</fetch>
Also, I don't know which CRM version are you using but I'm pretty sure that this plugin can be replaced by a rollup field so I'd give it a try!