Cancel Workflow Issue In Code
# help-with-umbraco


02/09/2024, 4:14 PM
Hi all, I'm using Umbraco Workflow and trying to initiate and cancel a workflow using code. I've got the initiate action working but unfortunately can't for the life of me figure out what the requirements are to cancel one. The cancel one returns the error "InvalidOperationException: Sequence contains no elements". I've look for some documentation and the code on github to help me diagnose the issue but couldn't find any. Any help would really be appreciated. Thanks // Initiate workflow, this works private void StartWorkflowProcess(IContent content) { var workflowInstance = new Umbraco.Workflow.Core.Models.InitiateWorkflowRequestModel { NodeId = content.Id, Publish = true, Comment = "sdfsfs", VariantToInitiate = "*" }; var initiationResult = _workflowService.InitiateWorkflow(workflowInstance); // Handle the initiation result if (!initiationResult.Result.result.Success) { throw new Exception($"Failed to initiate workflow: {initiationResult.Exception}"); } } // Cancel workflow, this doesn't work private void CancelWorkflowProcess(IContent content) { var workflowInstance = new Umbraco.Workflow.Core.Models.Pocos.WorkflowInstancePoco { NodeId = content.Id, AuthorUserId = 1, Variant = "*", AuthorComment = "fff", CompletedDate = DateTime.Now, }; var workflowRequest = new Umbraco.Workflow.Core.Models.ActionWorkflowRequest { UserId = 1, AssignTo = "Test", Comment = "Test", GroupId = 3, TaskId = 42, Offline = false, InstanceGuid = new Guid("0D0A9A5E-6188-4775-8F85-5A6452BA7FDE") }; var initiationResult = _workflowService.CancelWorkflow(workflowInstance, workflowRequest); // Handle the initiation result if (!initiationResult.Result.result.Success) { throw new Exception($"Failed to initiate workflow: {initiationResult.Exception}"); } }


02/09/2024, 8:09 PM
Hey @lordcererbus to cancel the workflow, you need the instance guid, use that to get the actual workflow instance (there's a method on the workflow service), then call Cancel workflow on workflow service


02/09/2024, 9:16 PM
OK thanks very much I'll give it a go tomorrow. Cheers