Counting Total number of records retrived by Fetch Xml using Custom workflow


We have done a custom workflow by using fetchxml to get the count of total opened tasks in CRM instance.

Script: 
protected override void Execute(CodeActivityContext context)
{
ITracingService tracingService = context.GetExtension<ITracingService>();
IWorkflowContext workflowContext = context.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory =       context.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(workflowContext.UserId);

String query = @"<fetch version='1.0' mapping='logical' distinct='false' >
<entity name='task'>
<attribute name='subject' />
<attribute name='createdby' alias='countAlias' />
<order attribute='subject' descending='false' />
<filter type='and'>
<condition attribute='statecode' operator='eq' value='0' />
</filter>
</entity>
</fetch>";

FetchExpression fetch = new FetchExpression(query);
EntityCollection results = service.RetrieveMultiple(fetch);
int count = results.Entities.Count; //show this in your message box;
this.count.Set(context, count);

var countname = ((AliasedValue)count.["createdby"]).Value;
systemuser.Set(context, countname);
}
       [Output("Count")]
public OutArgument<int> count { get; set; }

}

}





No comments:

Post a Comment