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 🙂

Published by arpitpowerguide

My name is Arpit Shrivastava, who is a Microsoft MVP in the Business Applications category. I am a Microsoft Dynamics 365 and Power Platform enthusiast person who is having a passion for researching and learning new things and acquiring immense knowledge. I am providing consistent help, support, and sharing my knowledge through various Social Media Channels along with my Personal Blog, Microsoft Community, conducting online training and attending various 365 Saturday Events worldwide and sharing the best Solutions to the readers helping them achieve their goals and objectives in Customer Relationship Space.

One thought on “Fire a Workflow in Dynamics CRM 2011 using C#

  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.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: