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


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”


Here is screenshot of registered plugin step


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));

                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>();

                        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;



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: Logo

You are commenting using your 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