Thursday, July 7, 2011

Fun with CRM RetrieveMultiple, ConditionOperator.Contains and sql indexing

Had an interesting problem today, trying to search a CRM entity.
I had the following code:
QueryExpression query = new QueryExpression("fpis_jobsite")
{ 
    ColumnSet = new ColumnSet { AllColumns = true } 
};
query.Criteria = new FilterExpression();
query.Criteria.AddCondition("fpis_name", ConditionOperator.Contains, siteName);
EntityCollection entities = organizationService.RetrieveMultiple(query);
return entities.Entities.Select(BuildJobSite).ToList();

Running it in a test gave me a Generic SQL Error.

After much fuss, I followed the instructions
here to run a SQL trace.

I found the following error:
"Cannot use a CONTAINS or FREETEXT predicate on table or indexed view 'fpis_JobSite' because it is not full-text indexed."
Once I changed the ConditionOperator.Contains operator in my condition to ConditionOperator.Like, the call executed without any further problems.
On a side note, it is not possible to populate child entities by calling RetrieveMultiple with a QueryExpression. You have to make seperate web service calls. In most cases though, you'd be able to craft your QueryExpression to retrieve all the child entities in a single call.