widmayer
widmayer

Reputation: 327

Dynamics CRM SDK - Cancel Workflow

I am trying to find a way to cancel a workflow using the Dynamics CRM SDK. Currently I can retry a workflow, but I am having issues being able to cancel one programatically. Is this possible?

Upvotes: 0

Views: 2394

Answers (2)

Richard Dewhirst
Richard Dewhirst

Reputation: 229

To Start a workflow:

ExecuteWorkflowRequest request = new ExecuteWorkflowRequest()
                {
                    WorkflowId = Workflow.Id,
                    EntityId = entity.Id
                };


                // Execute the workflow.
                ExecuteWorkflowResponse response =
                    (ExecuteWorkflowResponse)service.Execute(request);

To End:

Entity operation = new Entity("asyncoperation")
{
    Id = WorkflowRef.Id
};

operation["statecode"] = new OptionSetValue(3);
operation["statuscode"] = new OptionSetValue(32);

organizationservice.Update(operation);

Upvotes: 3

James Wood
James Wood

Reputation: 17562

Have you seen Asynchronous Operation States?

Apparently you just need to make an update call of the statecode.

Retrieving and Updating AsyncOperation States

Monitoring and updating the state of an asynchronous operation is typically done interactively through the System Jobs grid in the Microsoft Dynamics CRM Web application. However, you can also use the SDK to write code that performs those same tasks.

Read the state of an asynchronous operation

  1. Retrieve an AsyncOperation by name using the RetrieveMultiple method or by ID using the Retrieve method.

  2. Read the AsyncOperation.StateCode attribute.

Change the state code

  1. Modify the retrieved state code attribute to a new value according to the allowed operation states. You could also change the AsyncOperation.PostponeUntil attribute.

  2. Call Update to change the value of those attributes in the database.

Upvotes: 1

Related Questions