Monday, 19 September 2011

Cancelling workflows via the SDK

Hi guys,

Quick query

Does anyone know if it’s possible to cancel workflows via the SDK in CRM 2011? I find this hugely useful for cancelling long running workflows when a contacts state changes.

The CRM 4.0 code has been changed to crm 2011 style and throws the error:

[System.ServiceModel.FaultException] = {"The 'SetStateDynamicEntity' method does not support entities of type 'asyncoperation'."}

SetStateRequest request = new SetStateRequest();
request.State = new OptionSetValue(3); // completed
request.Status = new OptionSetValue(AsyncOperationStatus.Canceled);
request.EntityMoniker = new EntityReference("asyncoperation", singleWorkflowInstance.Id);
SetStateResponse updated = (SetStateResponse) CRMInstance.Execute(request);

Got it guys:

Set state is not supported, but strangely a manual update works:

Entity workflowToCancel = new Entity("asyncoperation");

workflowToCancel.Id = workflowID;
workflowToCancel["statecode"] = new OptionSetValue(3);
workflowToCancel["statuscode"] = new OptionSetValue(32);


I eventually found this article on it which mentioned that an update works

possible statuscodes:

Canceled = 32;
Canceling = 22;
Failed = 31;
InProgress = 20;
Pausing = 21;
Succeeded = 30;
Waiting = 10;
WaitingForResources = 0;

Not sure on the rest of the state codes but I’m sure they would be reasonably simple

1 comment:

  1. Thanks so much. Been tearing my hair out all afternoon trying to figure this out.