First code that I wrote used SetState message to Cancel change state of workflow instance but during first lunch I’ve got an exception that SetState message is not applicable for AsyncOperation entity. I went to SDK and found following explanation:
Updates an asynchronous operation (system job). Typically, you would update only the AsyncOperation.StateCode or AsyncOperation.PostponeUntil attributes of the asynchronous operation (system job). You can also call the IOrganizationService.Update method.
So I rewrote my code using Update message:
Entity operation = new Entity("asyncoperation") { Id = <Put identifier of Workflow Instance here> }; operation["statecode"] = new OptionSetValue(3); operation["statuscode"] = new OptionSetValue(32); organizationservice.Update(operation);
UPD: Here is the code that allows to postpone asyncoperation till some date:
Entity operation = new Entity("asyncoperation") { Id = <Put identifier of Workflow Instance here> }; operation["postponeuntil"] = DateTime.Now.AddDays(1); operation["statecode"] = new OptionSetValue(1); operation["statuscode"] = new OptionSetValue(10); organizationservice.Update(operation);
A tool exist for canceling workflows : http://www.gapconsulting.co.uk/our-solutions/free-tools/workflow-executor
Yannick,
Thanks for reference but what if I need to cancel workflow inside plugin or custom workflow activity? I believe that I can use code that I published but solution you sent a reference will be unusable.
Andrii