Search code examples
c#dynamics-crmdynamics-crm-4

'Unexpected error occurred Platform' - could this be due to incorrectly querying a picklist?


I'm trying to return a list of 'team members' from a custom entity, where their role matches a list of specific values. The attribute I'm trying to match to is a picklist and I'm wondering if this is causing my error. Here's my code:

public static BusinessEntityCollection GetTeamMembers(string BU, string[] roles)
{
    BusinessEntityCollection TeamMembers = new BusinessEntityCollection();

    ColumnSet cols = new ColumnSet();
    cols.Attributes = new string[] { "new_teamid", "new_id", "new_name", "ownerid", "new_rolepicklist" };

    QueryExpression query = new QueryExpression();
    query.ColumnSet = cols;
    query.EntityName = EntityName.new_team.ToString();

    List<ConditionExpression> ceList = new List<ConditionExpression>();

    foreach (string role in roles)
    {
        //I suspect it this block is where the problem occurs as 'new_rolepicklist' is a picklist, and 'role' will be something like 'Manager' (a picklist value).
        ConditionExpression ce2 = new ConditionExpression();
        ce2.AttributeName = "new_rolepicklist";
        ce2.Operator = ConditionOperator.Like;
        ce2.Values = new string[]{role};

        ceList.Add(ce2);
    }

    FilterExpression filter = new FilterExpression();
    filter.Conditions = ceList.ToArray();
    filter.FilterOperator = LogicalOperator.Or;
    query.Criteria = filter;

    try
    {
        using (crmService)
        {
            TeamMembers = crmService.RetrieveMultiple(query); //ERROR
        }
    }
    catch (SoapException se)
    {
        throw new Exception("Error occurred retrieving Team Members for " + BU + ". " + se.Detail.InnerXml);
    }
    catch (Exception ex)
    {
        throw new Exception("Error occurred retrieving Team Members for " + BU + ". " + ex.Message);            
    }

    return TeamMembers;
}

Firstly, is there anything glaringly obvious that's likely to be causing this error? Secondly, if it's the picklist part how do I fix it? How can I query an attribute that's a picklist with a picklist value?

UPDATE

Following Alex's correct answer, for completion I edited my method to take int[] roles as well as the foreach (int role in roles) and ce2.Values = new object[] {role} (an int[] is not allowed here).


Solution

  • Your code is choking when it comes to comparisons: Picklist values are of type int but you are feeding strings. Once types match it should work.