Moving BPF Stages from Plugin – Method 1

Sometimes a requirement from client comes to dynamically move stages of BPF based on some field value change on entity form. So, here I am showing the same implementation using plugin.

Note: Click here to download complete code of the plugin.

From Dynamics 365, Microsoft imposed restriction to move stages forward by skipping some stages in between. In previous versions, it was unsupported but no restriction was imposed by Microsoft. For example we have four stages; Qualify, Develop, Propose and Close. In Dynamics 365, if we move forward from stage Qualify to Propose, CRM will throw error with code “-2146885629” as per design. You have to move to next adjacent stages one by one to reach at your desired stage. You cannot jump to more than one stage in forward direction directly. See this link for reference. I have handled this restriction in my plugin. If user tries to skip some stages between two stages of BPF, the plugin will throw error message like this

1.png

I have registered the plugin (named ChangeBPFStageOnFieldChange) on an entity “new_testentity” on pre operation on Update message. The plugin will fire on change of field “new_stagename” which is an optionset containing options with names of each stage of BPF.

Here is screenshot of the entity along with BPF and the field “new_stagename”

2.png

Here is screenshot of registered plugin step

3.png

Code of Plugin’s Execute method looks like this

public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            //IOrganizationService crmService = serviceFactory.CreateOrganizationService(context.UserId);
            IOrganizationService crmService = serviceFactory.CreateOrganizationService(null);
            ITracingService trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            try
            {
                if (context.MessageName.ToLower().Equals("update") && context.PrimaryEntityName.ToLower().Equals("new_testentity"))
                {
                    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                    {
                        //--- Create collection of BPF stages
                        BPFStages = new List<BPFStage>();
                        LoadBPFStagesList();

                        Entity entity = (Entity)context.InputParameters["Target"];
                        if (entity.Contains("new_stagename") && entity["new_stagename"] != null)
                        {
                            int stageValue = ((OptionSetValue)entity["new_stagename"]).Value;
                            MoveBPFStage(context, stageValue, entity, crmService, trace);
                        }
                    }
                }
            }
            catch (InvalidPluginExecutionException ex)
            {
                throw ex;
            }
        }

References:

https://community.dynamics.com/365/b/dynamicscrmconsultant/archive/2017/03/29/invalid-stage-transition-transition-to-stage-0-is-not-in-the-process-active-path

https://stackoverflow.com/questions/29618316/crm-2015-online-business-process-flow-plugin-error

https://msdn.microsoft.com/en-us/library/dn481586.aspx#ManageBPF

https://community.dynamics.com/crm/b/develop1/archive/2017/03/13/there-is-something-rather-different-about-dynamics-365-business-process-flows

Advertisements

Author: M. Fasih Akbar

I have 12+ years experience in software industry mainly with Microsoft Technology stack. I have 7+ years of experience with ASP.Net. I am working with Microsoft Dynamics CRM since last 4+ years. Throughout my software development career, I have been engaged in project analyses, as well as in designing applications and databases. My major professional expertise belongs to object oriented web-based technologies like Dynamics CRM, Asp.Net, C#, JQuery, Angular JS, Knockout, Javascript, VB.Net, and MVC.

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