Answered by:
How to stop existing Workflow instance before firing another instance of the same workflow

Question
-
How to stop existing Workflow instance before firing another instance of the same workflow
Example:
Start:
Wait for Next Due Date == Today
Reschedule Activity Accordingly
Recursive Call: Auto Reschedule Activity
End
What i am looking for is:
Start:
Check if there is an existing workflow with same Workflow ID, Entity ID and in Waiting State.
Then Stop that workflow.
Wait for Next Due Date == Today
Reschedule Activity Accordingly
Recursive Call: Auto Reschedule Activity
EndMadhu M.
Saturday, May 5, 2012 1:05 AM
Answers
-
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
//Condition for WorkflowName
ColumnSet cols = new ColumnSet();
ConditionExpression condworkflowname = new ConditionExpression();
condworkflowname.AttributeName = "name";
condworkflowname.Operator = ConditionOperator.Equal;
condworkflowname.Values.Add("Update Influencer Status based on Start&End Date in Awards");
//Condition for Statuscode
ConditionExpression condworkflowstatuscode = new ConditionExpression();
condworkflowstatuscode.AttributeName = "statuscode";
condworkflowstatuscode.Operator = ConditionOperator.Equal;
condworkflowstatuscode.Values.Add(10);
ConditionExpression condregobjectid = new ConditionExpression();
condregobjectid.AttributeName = "regardingobjectid";
condregobjectid.Operator = ConditionOperator.Equal;
condregobjectid.Values.Add(context.PrimaryEntityId);
//Create the FilterExpression
FilterExpression filter = new FilterExpression();
//Set the properties
filter.FilterOperator = LogicalOperator.And;
filter.Conditions.Add(condworkflowname);
filter.Conditions.Add(condworkflowstatuscode);
filter.Conditions.Add(condregobjectid);
//Create the QueryExpression Object
QueryExpression query = new QueryExpression();
query.EntityName = "asyncoperation";
query.ColumnSet = cols;
query.Criteria = filter;
//Retrieve all the instance of waiting Workflow
EntityCollection asynOp = service.RetrieveMultiple(query);
//Check Response contains Entity
if (asynOp.Entities.Count > 0)
{
foreach (Entity _entity in asynOp.Entities)
{
//Retrive the response from entity
AsyncOperation async = (AsyncOperation)_entity;
async.StateCode = AsyncOperationState.Completed;
service.Update(async);
}
}Madhu M.
- Marked as answer by Madhu_M Wednesday, May 16, 2012 10:40 PM
Wednesday, May 16, 2012 10:40 PM
All replies
-
OOB such conditions can not be included through the workflow designer. You can however create a custom workflow assembly and incorporate this search using sdk calls.
HTH
Sam
Dynamics CRM MVP | Inogic | http://inogic.blogspot.com| news at inogic dot com
If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"
- Proposed as answer by Sam - Inogic Saturday, May 5, 2012 2:17 AM
Saturday, May 5, 2012 2:17 AM -
is it possible to do with Plugins??
Any sample code on this would be much appreciated in workflow assembly/Plugins
Madhu M.
Saturday, May 5, 2012 2:33 AM -
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
//Condition for WorkflowName
ColumnSet cols = new ColumnSet();
ConditionExpression condworkflowname = new ConditionExpression();
condworkflowname.AttributeName = "name";
condworkflowname.Operator = ConditionOperator.Equal;
condworkflowname.Values.Add("Update Influencer Status based on Start&End Date in Awards");
//Condition for Statuscode
ConditionExpression condworkflowstatuscode = new ConditionExpression();
condworkflowstatuscode.AttributeName = "statuscode";
condworkflowstatuscode.Operator = ConditionOperator.Equal;
condworkflowstatuscode.Values.Add(10);
ConditionExpression condregobjectid = new ConditionExpression();
condregobjectid.AttributeName = "regardingobjectid";
condregobjectid.Operator = ConditionOperator.Equal;
condregobjectid.Values.Add(context.PrimaryEntityId);
//Create the FilterExpression
FilterExpression filter = new FilterExpression();
//Set the properties
filter.FilterOperator = LogicalOperator.And;
filter.Conditions.Add(condworkflowname);
filter.Conditions.Add(condworkflowstatuscode);
filter.Conditions.Add(condregobjectid);
//Create the QueryExpression Object
QueryExpression query = new QueryExpression();
query.EntityName = "asyncoperation";
query.ColumnSet = cols;
query.Criteria = filter;
//Retrieve all the instance of waiting Workflow
EntityCollection asynOp = service.RetrieveMultiple(query);
//Check Response contains Entity
if (asynOp.Entities.Count > 0)
{
foreach (Entity _entity in asynOp.Entities)
{
//Retrive the response from entity
AsyncOperation async = (AsyncOperation)_entity;
async.StateCode = AsyncOperationState.Completed;
service.Update(async);
}
}Madhu M.
- Marked as answer by Madhu_M Wednesday, May 16, 2012 10:40 PM
Wednesday, May 16, 2012 10:40 PM