Tuesday 6 May 2014

Fire a Workflow in Dynamics CRM 2011 using C#

The following is a code sample that calls two methods: GetWorkflowId and ExecuteWorkFlow to run Workflow Programatically either C# or Plug-In :


One important thing to note is that the workflow we are executing must be marked as an on-demand or child workflow; otherwise you will receive an error below.

"Workflow must be marked as On Demand or Child Workflow"


 public void Execute(IServiceProvider serviceProvider)
        {
            // Get Workflow or Process Id
            // "Test_WF"  = Workflow Name
            // "workflow" = Process Schema name
            // "incident" = Entity Name on which workflow will run
            // "false"    = either workflow must be 'ondemand' or 'child workflow'
            //  1         = indicate Type of workflow 1-Activate, 2- Draft

            Guid processId = GetProcessId(service, "workflow", "Test_WF","incident",false,1).Id;


            // Execute Workflow or Process
            ExecuteWorkFlow(service, processId, new Guid("BF2184E0-51D4-E311-A7B2-7427EA0AF460"));
           

        }



 private Entity GetProcessId(IOrganizationService service,string Process,string WorkflowName,string EntityName,bool IsOnDemand,int Type)

                        //Internal method used to search for entity records
                        {
                     
                        QueryExpression query = new QueryExpression(Process);

                        query.ColumnSet = new ColumnSet(true);

                        query.Criteria.AddCondition(new ConditionExpression("name", ConditionOperator.Equal, WorkflowName));

                        query.Criteria.AddCondition(new ConditionExpression("type", ConditionOperator.Equal, Type));

                        query.Criteria.AddCondition(new ConditionExpression("primaryentity", ConditionOperator.Equal, EntityName));

                        query.Criteria.AddCondition(new ConditionExpression("ondemand", ConditionOperator.Equal, IsOnDemand));

                        try

                        {

                        EntityCollection response = service.RetrieveMultiple(query);

                        if (response != null && response.Entities.Count > 0)

                        return response.Entities[0];

                        else { return null; }

                        }

                        catch (Exception ex)

                        {

                        //Do error handling

                        throw ex;

                        }

                   }
          }





private void ExecuteWorkFlow(IOrganizationService service, Guid workFlowId, Guid entityId)
        {
            try
            {

                // Create an ExecuteWorkflow request.

                ExecuteWorkflowRequest request = new ExecuteWorkflowRequest()
                {

                    WorkflowId = workFlowId,

                    EntityId = entityId,

                };

                // Execute the workflow.

                ExecuteWorkflowResponse response =

                (ExecuteWorkflowResponse)service.Execute(request);

            }

            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException();
            }

        }


Cheerssss :)

1 comment:

  1. Special thanks to (hackingsetting50@gmail.com) for exposing my cheating husband. Right with me i got a lot of evidences and proofs that shows that my husband is a fuck boy and as well a cheater ranging from his text messages, call logs, whats-app messages, deleted messages and many more, All thanks to

    (hackingsetting50@gmail.com), if not for him i will never know what has been going on for a long time.

    Contact him now and thank me later.

    ReplyDelete

Blogger Widgets