views:

1018

answers:

3

Hey guys,

I'm trying to work out how to use the CrmService to close a Task in MS CRM 4.0

I've tried to use the SetStateTaskRequest to set a Task's state and status to TaskState.Completed and 5. I also tried TaskState.Completed and -1, but no dice there either.

Either way, I only receive the ever-helpful "Server was unable to process request" exception on the CrmService.Execute attempt.

I can create and update Tasks as freely as I please. But I can't seem to set them to completed. It's frustrating.

I noticed that I can only set the state of a Task to Completed in CRM through the Close Task action. I was wondering if there is a separate CrmService call that I need to make to perform the Close Task action, rather than going through the CrmService.Execute method.

Oh: I'm logging into the CrmService with full permissions. So I can't see that it would be a permissions issue on the task item.

I can't think what else could be causing this issue. Any advice or even just a point in the right direction would be very much appreciated.

FIRST EDIT:

Thanks to grega g for getting me to check the Detail field of the exception.

I now have a more detailed exception message. In XML Form:

<error>
    <code>0x80040203</code>
    <description>Invalid format of input XML for request SetStateTask: required field 'EntityId' is missing.</description>
    <type>Platform</type>
</error>

Which is bizarre - consider my code (almost identical to greg g's:

        SetStateTaskRequest request = new SetStateTaskRequest();
        request.EntityID = gTaskId;
        request.TaskState = TaskState.Completed;

        // ETaskStatusCode is an enumeration of the status codes taken from the StringMap in CRM.
        //
        // ETaskStatusCode.Completed = 5 - I can confirm this is the accurate status value for a Closed Task.
        //
        // Also, I have attempted this code with -1, which the documentation claims should cause the status
        // to automatically be set to the default status for the supplied state. No change there.
        request.TaskStatus = (int)ETaskStatusCode.Completed;            

        SetStateTaskResponse response = CRMManager.CrmService.Execute(request) as SetStateTaskResponse;

Also, just to confirm that I have the right status code (and also share something I've found very useful when dealing with MS CRM), here's the SQL I use to determine the values for entity statuses.

SELECT
    MSE.ObjectTypeCode,
    MSE.PhysicalName,
    SM.AttributeName,
    SM.Value,
    SM.AttributeValue
FROM MetadataSchema.Entity MSE
    INNER JOIN StringMap SM on MSE.ObjectTypeCode = SM.ObjectTypeCode
ORDER BY MSE.PhysicalName, SM.AttributeName, SM.AttributeValue

I can confirm from the MS CRM web interface that the Status value that is associated with a Completed task is also named Completed. I can confirm from the SQL above that the value of this status, for a Task, is 5 - this is the value passed in from my Enum.

I can also confirm that gTaskId is being set to a valid Guid that references a Task that actually does exist, and is open at the time the close is attempted.

Curiouser and curiouser. Any thoughts?

+1  A: 

Use SetStateTaskRequest class.

SetStateTaskRequest task2Close = new SetStateTaskRequest();
task2Close.EntityId = <taskGuid>
task2Close.TaskState = TaskState.Completed;
task2Close.TaskStatus = <some value>

try
{
    SetStateTaskResponse r = (SetStateTaskResponse) crmSvc.Execute(task2Close);
}
catch (SoapException e)
{
   //Use e.Details for more info than "server was unable ..."
}

This code should work (or let you see why error occurs)

grega g
First off the bat - that's almost *exactly* my code. But I hadn't realized I could get a more detailed message out of the Details field. However, the nature of that message is... weird, given the context. I'll edit my original post with the details.
Ubiquitous Che
A: 

Are you sure that when you are trying to close task you're passing status value which is valid for Completed state? Different status codes are only valid with their corresponding state codes. Can you add your source code and a portion of your state entity customization?

Dmitriy Matveev
Thanks for the suggestion - but yes, I'm sure. I ran some SQL on the StringMap to confirm. A completed task should have a status of 'Completed', which is set to 5 in the StringMap for tasks. I also tried -1, which the documentation states should give me the 'default' entry for the state chosen. No luck.
Ubiquitous Che
A: 

Found it!

Okay - reviewing my code above and the error message closely, my CrmService contained the property EntityID - but the exception was that the property EntityId was missing.

Somehow, my CrmService had its EntityId property renamed to EntityID.

Renaming the property fixed the problem. I still have no idea how that happened in the first place.

To be safe, I'll regenerate a new Service proxy to make sure that my properties are correctly named.

Looking through the code, it seems that someone did a find-and-replace on 'Id' and turned it into 'ID' - which incidentally is the naming convention in my workplace for Property fields that represent primary keys.

Thanks again to grega g for pointing out that the Detail property had the extra information I needed.

Ubiquitous Che