Wednesday, 18 March 2015

Get Workflow ID and Execute it in CRM 2011, 2013 and 2015 using C# Code

Here is the code for retrieving workflow id by name and issue execute request on it.

QueryExpression objQueryExpression = new QueryExpression("workflow");
objQueryExpression.ColumnSet = new ColumnSet(true);
objQueryExpression.Criteria.AddCondition(new ConditionExpression("name", ConditionOperator.Equal, "<WorkflowName>"));
objQueryExpression.Criteria.AddCondition(new ConditionExpression("parentworkflowid", ConditionOperator.Null));
EntityCollection entColWorkflows = service.RetrieveMultiple(objQueryExpression);
if (entColWorkflows != null && entColWorkflows.Entities.Count > 0)
{
       ExecuteWorkflowRequest request = new ExecuteWorkflowRequest()
       {
            WorkflowId = entColWorkflows.Entities[0].Id,
            EntityId = guidRecord
       };
       // Execute the workflow.
       ExecuteWorkflowResponse response = (ExecuteWorkflowResponse)service.Execute(request);
}

--
Happy CRM'img
Gopinath.
 

3 comments:

  1. Brother i am getting the response guid as all 0's does this shows that my workflow has executed ?

    ReplyDelete
  2. it it possible to trigger workflow without "EntityId = guidRecord"

    ReplyDelete