web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

BusinessProcessFlowInstance Plugin on PreUpdate stage

(0) ShareShare
ReportReport
Posted on by

Hi all,

I have a requirement from the customer to implement a validation logic on the opportunity business process when we go from stage to another. The logic is complex so for that reason we cant do it in the businessflow editor. For that reason Il trying to implement a plugin on the BusinessProcessFlowInstance entity on the PreUpdate message to get the Previous stage id and the target stage id (Next one). I have no problem with stages ids, but I need to get the opportunity id related to the current Businessflow instance to retrieve data related to the opportunity for my validation logic requirement. So my question is : Haw to get the opportunity from the BusinessProcessFlowInstance plugin.

bellow : the plugin code that I start to implement :

namespace REA.Crm.Plugins.OpportunityPlugins
{
    public class businessprocessflowinstancePlugin : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            //Extract the tracing service for use in debugging sandboxed plug-ins.
            ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));

            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            OrganizationServiceContext1 OrgServiceCntx = new OrganizationServiceContext1(service);
            BusinessProcessFlowInstance targetEntity = ((Entity)context.InputParameters["Target"]).ToEntity<BusinessProcessFlowInstance>();
            BusinessProcessFlowInstance preimage = ((Entity)context.PreEntityImages["preimage"]).ToEntity<BusinessProcessFlowInstance>();

            Guid? PreviousActiveStage = GetPreviousActiveStageFromTraversedPath(preimage.TraversedPath);
            Guid? TargetActiveStage = targetEntity.ProcessStageId;




            tracingService.Trace("EntityID " + targetEntity.Entity1Id.ToString());
            tracingService.Trace("EntityID " + targetEntity.Entity2Id.ToString());
            tracingService.Trace("EntityID " + targetEntity.Entity3Id.ToString());
            tracingService.Trace("EntityID " + targetEntity.Entity4Id.ToString());
            tracingService.Trace("EntityID " + targetEntity.Entity5Id.ToString());



        }

        Guid? GetPreviousActiveStageFromTraversedPath(string TraversedPath)
        {
            Guid? PreviousActiveStage = null;
            string[] splitedTraversedPath = TraversedPath.Split(',');
            if (splitedTraversedPath.Length > 0)
                PreviousActiveStage=Guid.Parse(splitedTraversedPath[splitedTraversedPath.Length].ToString());

            return PreviousActiveStage;
        }

    }
}

*This post is locked for comments

I have the same question (0)
  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    Hello,

    You might be know that now every BPF having an entity with same BPF name. In the BPF entity you should find the opportunity lookup . You can navigate to customization-> Customize the system-> and then expand the BPF entity -> Fields and find the Opportunity lookup schema name.

    Guid OppId = ((EntityReference)targetEntity.Attributes["BPFLookupOppFieldId"]).Id;

    In addition you can also try with JavaScript,  have a look  here sample custom validation.

    goutamdascrm.wordpress.com/.../how-to-prevent-next-stage-bpf-set-active-current-stage-based-on-sub-grid-record-count-or-any-type-of-condition-2

  • HEDI Profile Picture
    on at

    Thank you Goutam, its exactly what I need. Its really a huge designe change in CRM concerning the BPF.

  • MMK Profile Picture
    745 on at

    Hi @ Gautam i tried the JS thing it isn't working can you tell me any other way

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans