Call Workflow within Workflow in MS Dynamics CRM 2011/2013/2015/2016

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using Microsoft.Crm;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Workflow;
using Microsoft.Xrm.Sdk.Workflow.Activities;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Messages;

namespace cel_WorkflowActivities
{

    public sealed class runRelatedWorkflow : CodeActivity
    {
        [RequiredArgument]
        [Input("Workflow To Execute")]
        [ReferenceTarget("workflow")]
        public InArgument<EntityReference> workflowId { get; set; }

        [RequiredArgument]
        [Input("Related Entity Logical Name")]

        public InArgument<string> entityName { get; set; }

        [RequiredArgument]
        [Input("Attribute Logical Name")]
        public InArgument<string> attributeName { get; set; }


        protected override void Execute(CodeActivityContext executionContext)
        {
            StringBuilder sb = new StringBuilder("starting; ");

            try
            {

                Guid wFlowId = ((EntityReference)executionContext.GetValue(this.workflowId)).Id;
                string eName = executionContext.GetValue(this.entityName);
                string fName = executionContext.GetValue(this.attributeName);
                IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
                IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.InitiatingUserId);

                QueryByAttribute q = new QueryByAttribute(eName);
                q.Attributes.AddRange(fName, "statecode");
                q.Values.AddRange(context.PrimaryEntityId.ToString(), "Active");
                EntityCollection retrieved = service.RetrieveMultiple(q);

                sb.Append(string.Format("Retrieved {0} records; ", retrieved.Entities.Count.ToString()));
                foreach (Entity c in retrieved.Entities)
                {
                    ExecuteWorkflowRequest exec = new ExecuteWorkflowRequest();
                    exec.EntityId = c.Id;
                    exec.WorkflowId = wFlowId;
                    sb.Append(string.Format("Executing workflow on record with id {0}; ", c.Id.ToString()));
                    service.Execute(exec);
                }

                sb.Append("done");
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException("An error occurred running related workflows: " + ex.Message);
            }
        }
    }
}

Comments

Popular posts from this blog

Workflow Activity Input and Output Parameters and Data Types in MS CRM 2013/15/16

Get Workflow Guid in Javascript using OData query

Convert Plugin to Sandbox mode,