2

I'm trying to retrieve records that contain some optionsetvalue from multi option set pick list.

This line make my code fail:

int[] values = GetValues();
query.Criteria.AddCondition(new ConditionExpression(fieldName, 
ConditionOperator.ContainValues, values);

When I put the values with a comma separator the code works:

query.Criteria.AddCondition(new ConditionExpression(fieldName, 
ConditionOperator.ContainValues, 1,2,3);

Only problem is that the values are dyanamic so I can't do that. The only way I found so far to overcome it was this:

FilterExpression filter = new FilterExpression(LogicalOperator.Or);
foreach (int value in values)
{
    filter.AddCondition(new ConditionExpression(fieldName, 
        ConditionOperator.ContainValues, value));
}
query.Criteria.AddFilter(filter);

Just wondering if there is a better solution than this one?

James Wood
  • 17,286
  • 4
  • 46
  • 89
Moran Barzilay
  • 133
  • 2
  • 13

2 Answers2

0

You could try using In.

Query data from multi-select picklists

With the Web API there are the equivilent ContainValues and DoesNotContainValues query functions.

Other existing condition operators that can be used with this type of attribute include: Equal, NotEqual, NotNull, Null, In and NotIn.

James Wood
  • 17,286
  • 4
  • 46
  • 89
  • In is not the correct way, and it doesn't solve the exception problem. You can even see in the documentation: int[] hikingValue = new int[] { 2 }; ConditionExpression condition = new ConditionExpression("sample_outdooractivities", ConditionOperator.ContainValues, hikingValue); the problem is that this part of code throw exception... – Moran Barzilay Aug 21 '18 at 08:53
0

The documentation obviously needs to be updated. The method definition takes an object array:

public void AddCondition(string attributeName, ConditionOperator conditionOperator, params object[] values)

so it should work if you change your values array to:

object[] values = GetValues();

This works in my code:

object[] roleValues = new object[] { (int)optionset_value };
Khoait
  • 328
  • 4
  • 18