Monday 12 May 2014

CRM 2011: How to Cancel/Terminate Workflow Programmatically using C#

I was assigned a task where workflow was running on status change and then it waits for few days to send an email message to contact. During the period of wait if status has been changed to something else then workflow should be cancelled.

So I have written a plugin with following code, which check for the status. if status is changed then cancel the workflow as following. There is an easy way to code by hard-coding the process Guid but I tried to write generic code so that I can reuse it if needed.

        /// 
        /// Cancel the running workflow if it is still not completed
        /// 
        /// Organization Service Context
        /// Regarding Entity Id
        /// Name of the workflow
        /// Logical name of the Entity on which worklow is running
        private static void CancelRunningWorkflow(OrganizationServiceContext context,string entityLogicalName, Guid regardingObjectId, string processName)
        {
           // Get Process by name where its running on given incident
           // and current status is waiting, in process or waiting for resources
            var processes = (from p in context.CreateQuery<AsyncOperation>()
                where p.PrimaryEntityType == entityLogicalName
                      && p.RegardingObjectId.Id == regardingObjectId
                      && p.Name == processName
                      &&
                      (p.StatusCode.Value == 10 ||       // Waiting
                       p.StatusCode.Value == 20 ||       // In Process
                       p.StatusCode.Value == 0)          // Waiting For Resources
                select new AsyncOperation {Id = p.Id, StateCode = p.StateCode, StatusCode = p.StatusCode})
                .ToList();

           // Go through each process and set the status to Cancelled
            foreach (var process in processes)
            {
                process["statecode"] = new OptionSetValue(3);
                process["statuscode"] = new OptionSetValue(32);       // Cancelled
                context.UpdateObject(process);
            }
            // Save the changes
            context.SaveChanges();
        }
You can call the method as below:
CancelRunningWorkflow(context, Incident.EntityLogicalName, incident.Id, "Workflow to Stop");
You can create linq Context as following:
OrganizationServiceContext context = new OrganizationServiceContext(_service);

Note: Make sure use have permissions to cancel the process.

Happy Coding

P. S. Hayer
(ਪ੍ਰੇਮਜੀਤ ਸਿੰਘ ਹੇਰ)

Please check my other (non-CRM) blog here: Programming Blogs

No comments :

Post a Comment