Search code examples
c#dynamics-crmxrmquery-expressions

QueryExpression in a field that shouldn't be null and has particular value


I am trying to write a QueryExpression in C# using the CRM SDK. Where I have a main query and some entities to join. My issue is that 1 of the entities that I join to has a field that I need to filter and cannot be null also.

var mainQuery = new QueryExpression([entity1])
{
    ColumnSet = new ColumnSet("XXXX"),
};
var entity1Join = mainQuery.AddLink([fields], JoinOperator.Inner);

...[other joins here]

var entity2Join = entity1Join.AddLink("entity2", "fieldid", "fieldid", JoinOperator.Inner);
contactJoin.LinkCriteria = new FilterExpression()
{
    FilterOperator = LogicalOperator.And,
    Conditions = { 
            new ConditionExpression([field1], ConditionOperator.NotNull), 
            new ConditionExpression([field1], ConditionOperator.Equal, [value])
        }   
};
...[other joins here]


var result = this.CRMClient.OrganizationServiceProxy.RetrieveMultiple(mainQuery).Entities;

I get this error:

Condition for attribute 'entity2.field1': null is not a valid value for an attribute. Use 'Null' or 'NotNull' conditions instead.

Please help, thanks.


Solution

  • From your 2nd entity filter operation remove 1st condition for null because anyhow you are giving some specific value and also what is your value coming. Does your value itself is null or it does contain data.

    Before giving value for 2nd condition better check if it does contain data or not.