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

Friday 9 May 2014

CRM 2013: How to disable of "Get CRM for Outlook" notification bar permanently

Recently few of our users complained about "Get CRM for Outlook" notification they receive every time they login to CRM. However it is not as frustrating as popups but some laptop users feel that it takes little screen space and it is bit frustrating to close it every time. After spending little time I discovered that we can get rid of time as following.


  • Login to the system as System Administrator and go to Settings > Administration

  • Click on System Settings. It will open System settings in new window. 

  • Under out look tab go to the last option "Set whether users see CRM for Outlook message". Select 'No' and click OK.



Users will not get the "Get CRM for Outlook" message again, unitl you want it.

Same Steps can be followed to remove this message from CRM 2011 as well.

P. S. Hayer
(ਪ੍ਰੇਮਜੀਤ ਸਿੰਘ ਹੇਰ)
Please check my other (non-CRM) blog here: Programming Blogs