Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CRM 2011, Stopping custom workflow programmatically

i've been trying to stop a workflow programmatically. I've read both in various posts and in the msdn that this can be done by updating the Asyncoperation status via update request. However everytime i update the request. the workflow get stuck on a mid stage such as cancelling or pausing and dosen't reach a final state.

any ideas?

protected void ExecutePostAccountUpdate(LocalPluginContext localContext)
    {
        if (localContext == null)
        {
            throw new ArgumentNullException("localContext");
        }

        string fetchXML = "<fetch mapping='logical' count='50' version='1.0'>" +
                          "<entity name='asyncoperation'>" +
                          "<filter>" +
                          "<condition attribute='regardingobjectid' operator='eq' value='" +
                          localContext.PluginExecutionContext.PrimaryEntityId + "' />" +
                          "</filter>" +
                          "</entity>" +
                          "</fetch>";
        EntityCollection col = localContext.OrganizationService.RetrieveMultiple(new FetchExpression(fetchXML));
        if (col.Entities.Count > 0)
        {
            AsyncOperation a = (AsyncOperation)col[0];
            a.StateCode = AsyncOperationState.Completed;
            a.StatusCode = new OptionSetValue(32);
            localContext.OrganizationService.Update(a);



        }

    }
like image 682
Eranser Avatar asked Jun 07 '26 04:06

Eranser


1 Answers

Have a look at my blog: How to Cancel Workflow Programmatically using C#

Make sure the user have permissions to Cancel System Jobs.

like image 90
Scorpion Avatar answered Jun 10 '26 08:06

Scorpion



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!