internal EntityReference fetchCaseByCaseNumber(string caseNumber, IOrganizationService service, ITracingService tracingService)
{
Entity incident;
//tracingService.Trace("Creating QueryByAttribute");
//QueryByAttribute querybyattribute = new QueryByAttribute("incident");
//querybyattribute.ColumnSet = new ColumnSet("incidentid", "ticketnumber");
//querybyattribute.Attributes.AddRange("ticketnumber");
//querybyattribute.Values.AddRange(caseNumber);
//EntityCollection incidents = service.RetrieveMultiple(querybyattribute);
tracingService.Trace("Creating Query...");
QueryExpression query = new QueryExpression
{
EntityName = "incident",
//PageInfo = new PagingInfo() {count },
ColumnSet = new ColumnSet("incidentid", "ticketnumber"),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression
{
AttributeName = "ticketnumber",
Operator = ConditionOperator.Equal,
Values = {caseNumber}
}
}
}
};
tracingService.Trace("Running Query");
EntityCollection incidents = service.RetrieveMultiple(query);
tracingService.Trace("fetching total count");
int count = incidents.TotalRecordCount;
// incident = incidents.Entities.First();
if (count == 1)
{
incident = incidents.Entities.First();
}
else
{
tracingService.Trace(String.Format("{0} incidents found with ticketnumber",incidents.TotalRecordCount.ToString()));
throw new InvalidPluginExecutionException("Duplicate ticket number error");
}
EntityReference caseReference = new EntityReference("incident",incident.Id);
return caseReference;
}
SELECT TicketNumber, *
FROM IncidentBase
WHERE TicketNumber = '000003'
internal EntityReference fetchCaseByCaseNumber(string caseNumber, IOrganizationService service, ITracingService tracingService)
{
Entity incident;
tracingService.Trace("Creating Query...");
QueryExpression query = new QueryExpression
{
EntityName = "incident",
PageInfo = new PagingInfo() { ReturnTotalRecordCount = true },
ColumnSet = new ColumnSet("incidentid", "ticketnumber"),
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression
{
AttributeName = "ticketnumber",
Operator = ConditionOperator.Equal,
Values = {caseNumber}
}
}
}
};
tracingService.Trace("Running Query");
EntityCollection incidents = service.RetrieveMultiple(query);
tracingService.Trace("fetching total count for incidents with ticket number: {0}", caseNumber);
if (incidents.TotalRecordCount == 1)
{
incident = incidents.Entities.First();
}
else
{
tracingService.Trace(String.Format("{0} incidents found with ticketnumber",incidents.TotalRecordCount.ToString()));
throw new InvalidPluginExecutionException("Duplicate ticket number error");
}
EntityReference caseReference = new EntityReference("incident",incident.Id);
return caseReference;
}
You need to set ReturnTotalRecordCount property to true as per this MSDN article: https://msdn.microsoft.com/en-us/library/gg334688.aspx