Search code examples
pluginsdynamics-crmworkflow

Calling Action From Javascript Fails MSCRM


I have a few things that work together to complete multiple task from a queue.

The first is javascript which calls an Action (Workflow) handing a CSV of task ids and the users id. This action hands it all over the a plugin which loops the tasks, picks them for the user and then sets them to complete.

This all runs great unless a task is already assigned. I then get an error from the javascript.

enter image description here

At first this was saying "Item has already been assigned to another user". However I added a try catch as such

try
{
    Entity queueItem = RetrieveQueueItemIdByObjectId(service, new Guid(taskId));

    // Create the Request Object and Set the Request Object's Properties
    PickFromQueueRequest pickFromQueueRequest = new PickFromQueueRequest
    {
        QueueItemId = queueItem.Id,
        WorkerId = new Guid(_UserId)
    };
    // Execute the Request
    service.Execute(pickFromQueueRequest);
}
catch(FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
{
    if (ex.Detail.ErrorCode != -2147220891)
    {
        throw ex;
    }
}

I have tried that catch with

if(!ex.Message.Contains("Item has already been assigned to another user"))
{
    throw ex;
}

and just catching everything. And yet the error still shows. I then noticed the tasks were completing, but the error is getting thrown. After this code is where the task is set to complete. Which works because if the task is not already picked, no error is thrown.


Solution

  • The answer is in the error: in a plugin you cannot catch exceptions thrown by the OrganizationService (or any exception actually, as far as I'm aware of) and continue running.

    You have to expand your code so it doesn't throw if you want it to work. No way around this.