Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

BusinessProcessFlowInstance Plugin on PreUpdate stage

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

  • MMK Profile Picture
    MMK 745 on at
    RE: BusinessProcessFlowInstance Plugin on PreUpdate stage

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

  • HEDI Profile Picture
    HEDI on at
    RE: BusinessProcessFlowInstance Plugin on PreUpdate stage

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

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: BusinessProcessFlowInstance Plugin on PreUpdate stage

    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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans