Skip to main content

Cancel Workflow job programmatically - CRM v.4

How many times you had to created a workflow on some date like Due date which keeps changing as per client's request. While this may sound simple in business, on CRM you have a workflow that may be waiting till this date and send a reminder exactly a week before.

So you need to keep calling this workflow every time the date is updated, to make the matters more complicated, all the previous jobs that had initiated due to past modification will keep running and chasing that date which is never going to occur. What you do now, manually cancel those to save system resources and stop the never ending system jobs.

There is a way to do it through SDK, in my case I call this function as a custom workflow activity in the first step of the workflow which Cancels any previous jobs of this workflow.

  #region Cancel My WFs

            QueryExpression qe = new QueryExpression();
            qe.EntityName = EntityName.asyncoperation.ToString();
            ColumnSet cs = new ColumnSet();
            cs.Attributes = new string[] { "name", "regardingobjectid", "createdon","statuscode","statecode" };
            FilterExpression fe = new FilterExpression();
            fe.FilterOperator = LogicalOperator.And;

            ConditionExpression ce1 = new ConditionExpression();
            ce1.Operator = ConditionOperator.Equal;
            ce1.AttributeName = "regardingobjectid"; // Your record id
            ce1.Values = new object[] { new Guid("EE87FB7E-6C1F-E111-AF92-0050569F0AE2") };

            ConditionExpression ce2 = new ConditionExpression();
            ce2.Operator = ConditionOperator.Equal;
            ce2.AttributeName = "name"; // GUID of the Waiting Workflow: WF_InDeal_NextEndofTermNotificationDate_Update
            ce2.Values = new object[] { "Your_WF_Name" };

            ConditionExpression ce3 = new ConditionExpression();
            ce3.Operator = ConditionOperator.NotEqual;
            ce3.AttributeName = "statuscode"; // GUID of the Waiting Workflow: WF_InDeal_NextEndofTermNotificationDate_Update
            Status CancelledStatus = new Status(); CancelledStatus.Value = 32;
            ce3.Values = new object[] { CancelledStatus };

            fe.Conditions = new ConditionExpression[] { ce1,ce2};
            fe.FilterOperator = LogicalOperator.And;

            qe.ColumnSet = cs;
            qe.Criteria = fe;

            req = new RetrieveMultipleRequest();
            req.ReturnDynamicEntities = true;
            req.Query = qe;
            RetrieveMultipleResponse resp = null;
            resp = (RetrieveMultipleResponse)serv.Execute(req);
            if (resp.BusinessEntityCollection.BusinessEntities.Length  > 0)
                BusinessEntityCollection bec = resp.BusinessEntityCollection;
                asyncoperation asyncOperation = null;
                DynamicEntity dynamicEntity = null;
                TargetUpdateAsyncOperation targetUpdateAsyncOperation = null;
                string Status = "";
                for (int i = 0; i < bec.BusinessEntities.Length-1 ; i++)
                    Status = ((UpdateProductBasePrice.crmService.StatusProperty)(((UpdateProductBasePrice.crmService.DynamicEntity)(bec.BusinessEntities[i])).Properties[4]));
                    if (!Status.Equals("Canceled"))
                        asyncOperation = new asyncoperation();
                        dynamicEntity = (DynamicEntity)bec.BusinessEntities[i];
                        asyncOperation.asyncoperationid = new Key();
                        asyncOperation.asyncoperationid.Value = new Guid(((UpdateProductBasePrice.crmService.KeyProperty)(dynamicEntity.Properties[5])).Value.Value.ToString());
                        asyncOperation.statuscode = new Status();
                        asyncOperation.statuscode.Value = 32;
                        asyncOperation.statecode = new AsyncOperationStateInfo();
                        asyncOperation.statecode.Value = AsyncOperationState.Completed;

                        targetUpdateAsyncOperation = new TargetUpdateAsyncOperation();
                        targetUpdateAsyncOperation.AsyncOperation = asyncOperation;

                        UpdateRequest updateAsync = new UpdateRequest();
                        updateAsync.Target = targetUpdateAsyncOperation;

                        catch (System.Web.Services.Protocols.SoapException soapEx)



alfred said…
All you have to do to get this free service is to sign-up on their site and then paste a bit of javascript in your site's footer, so that accesses to all pages get metered. Each account you open can only cover one site, thus if you have ten sites, you'll have to register ten times.

javascript jobs

Popular posts from this blog

Microsoft reuse windows media player toolbar in Windows 7

Those who have been using Windows Media Player would have experienced one thing over time, when you click on the little up-down arrow heads on the right, you could see a thumbnail live view of the current movie you were playing. This had been since Window XP and now Microsoft have copy-pasted this code to show live preview of each taskbar window like a folder, browser session, office documents and every possible item you can open and appear in task bar.  Those of you who haven't seen Windows 7, this is how taskbar preview looks like :

Capture and Handle event when one entity record is attached through a Many to Many relationship - MS CRM 4

If you have already dug up too much about trying to capture the event when an entity record in MS CRM is attached ~ linked to a record in many to many relationship. For example, if you have two entities : Supplier and Company in MS CRM where a supplier can do business with many companies and at the same time a Company can order stuff from many suppliers. What if you want to send some notification whenever a new supplier or an existing supplier is linked to a company; similarly you may want to send a notification when a Supplier signs a deal with 2-3 companies and gets linked to those. Some experts from MS CRM fraternity have suggested some ways to over ride this limitation of MS CRM that it does not expose any event for the above mentioned scenario, below is one of the examples: But making changes to the default plugin model of MS CRM to add Associate or Disassociate event is not so eas