views:

160

answers:

1

We're using MSCRM Dynamics, and we're trying to get all the children of a particular user. (A user has a manager, the manager has 'children'.) The following works, but throws an exception if the user has no children. This seems logical at first, maybe, but why not just return an empty set? And of all things, it throws a SoapException with a cryptic message of "Invalid Argument" (which is wrong) and the .Detail.InnerText says "0x80040203 The value passed for ConditionOperator.In is empty Platform". If you look at the corresponding Response class, it has a collection -- why not just leave it empty?

// Create the request object.
RetrieveAllChildUsersSystemUserRequest retrieve =
    new RetrieveAllChildUsersSystemUserRequest();

// Create the column set object that indicates the fields to be retrieved.
ColumnSet cols = new ColumnSet();
cols.EntityName = "systemuserid";

// Set the column set.
retrieve.ColumnSet = cols;

// Set the ID of the parent user.
retrieve.EntityId = context.UserId;

RetrieveAllChildUsersSystemUserResponse retrieved =
    new RetrieveAllChildUsersSystemUserResponse();

/// Execute the request.
/// Catches if user does not have children
/// (Check to see if user is manager)

try
{
    retrieved =
        (RetrieveAllChildUsersSystemUserResponse)crmService.Execute(retrieve);
}
catch (System.Web.Services.Protocols.SoapException e)
{
    throw new Exception(string.Format("{0}", e.Detail.InnerText));
}
+1  A: 

I agree, it should probably just return an empty result. My guess would be under the hood, some step of executing the request or preparing the response is translated into a QueryExpression. QueryExpressions blow up if you use a ConditionExpression that uses ConditionOperator.In and you pass it an empty list. So it may be something like it gets a list of child systemuser guids, which in some cases is an empty list, then tries to retrieve all attributes of the system users in that list using another QueryExpression and that is what throws the exception.

You can probably design a QueryExpression or FetchXML of your own that will net you the same results without the side effect of it throwing an exception when the list is empty, or just catch the exception and check for that particular error code and swallow it.

Matt
We're currently swallowing all SoapExceptions, which I really feel is a poor solution. I'll have to dig around in the expression to see if I can better isolate this case, if no better resolution can be found.
Thanatos