Activate the Stages of BPF with the help of Plugin

Create Business process flow with stages develop, testing , UAT, create 3 fields with name develop, testing , UAT all are two optionsets, if select the Develop then activate the develop stage, if select UAT then activate the UAT stage using plugin.

Step 1: Create an entity > In entity create a field with option set of develop, testing and UAT > now save and publish the entity.

check in CRM entity and option set field are created or not.

Step 2: Create a BPF > Go to Process and create a BPF in that select the create entity> create three stages name them as develop, testing and UAT. For these three stages take in data step take the option set field which you created.
 

Step 3: Now for Stages id in BPF Search "Get BPF stage id"  in google click on this link ==> https://www.cloudfronts.com/blog/dynamics-crm/get-stage-id-of-business-process-flow-stages/ 

After opening that URL for stage change you will find this url 

https://Test130.crm.dynamics.com/api/data/v9.0/processstages?$select=stagename&$filter=processid/workflowid eq 9128E4D1-1234-4852-ABD0-A63A6ECA5C5D

copy the above url and make the changes were i have marked with our CRM url and also change the id with BPF id


Changes made for url with CRM url and url id changes made with BPF id.

Copy this URL and search in google then you will get the stage id's of BPF for each stage.

Step 4: Prepare the plugin and paste the BPF Id's, schema name of BPF and also schema name of the field.

using Microsoft.Crm.Sdk.Messages;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Query;

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;


namespace CheckBPF

{

    public class UpdateBPF : IPlugin


    {

        public void Execute(IServiceProvider serviceProvider)


        {

            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

            IOrganizationService Service = serviceFactory.CreateOrganizationService(context.UserId);

            ITracingService tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));


            try

            {

                if (context.Depth == 1)


                {

                    string logicalNameOfBPF = "new_appstatecheck";  //new_appstatecheck


                    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)


                    {


                        Entity eMployOobject = (Entity)context.InputParameters["Target"];

                        Entity activeProcessInstance = GetActiveBPFDetails(eMployOobject, Service);


                        // getting option set value.

                        OptionSetValue Stagestatus = eMployOobject.Contains("effizz_statuschecking") ? (OptionSetValue)eMployOobject["effizz_statuschecking"] : null;


                        // creating object for the system bpf entity and passing the entity id from activeProcessInstance

                        Entity entBPF = new Entity(logicalNameOfBPF)


                        {

                            Id = activeProcessInstance.Id

                        };


                        if (Stagestatus.Value == 684200000)


                        {


                            // activestageid common filed for the all instacne because this is system genrarated entity

                            entBPF["activestageid"] = new EntityReference("processstage", new Guid("70c687cc-f92b-4792-8985-ff44f294cdf4"));//dev


                        }


                        else if (Stagestatus.Value == 684200001)


                        {

                            //processstage this the stage entity lookup data type in system entity, so we use the entity refrence

                            entBPF["activestageid"] = new EntityReference("processstage", new Guid("5694e881-aa7d-4099-abe2-655310b822f9"));//TESTING

                        }


                        else if (Stagestatus.Value == 684200002)


                        {

                            entBPF["activestageid"] = new EntityReference("processstage", new Guid("3e3115fa-ab6a-4877-bf0e-a7d50be1a0c1"));//uat

                        }


                        Service.Update(entBPF);


                    }


                }


            }


            catch (InvalidPluginExecutionException Ex)

            {

                throw Ex;

            }

        }


        public Entity GetActiveBPFDetails(Entity entity, IOrganizationService crmService)

        {

            Entity activeProcessInstance = null;

            RetrieveProcessInstancesRequest entityBPFsRequest = new RetrieveProcessInstancesRequest


            {

                EntityId = entity.Id,

                EntityLogicalName = entity.LogicalName

            };


            //getting system gerrated entity data with entity id.


            RetrieveProcessInstancesResponse entityBPFsResponse = (RetrieveProcessInstancesResponse)crmService.Execute(entityBPFsRequest);


            if (entityBPFsResponse.Processes != null && entityBPFsResponse.Processes.Entities != null)

            {

                activeProcessInstance = entityBPFsResponse.Processes.Entities[0];

            }

            return activeProcessInstance;

        }

    }

}

Step 5: After the plugin code go to Plugin registration then register assemble with .dll file and register a new step with update msg.


After adding this plugin, based on option set it will check about activate the stage. in below option set is testing then bpf stage also in Testing stage.









 

Comments

Popular posts from this blog

If any case created, then check for the same user how many cases are created with in 30 days, if more than 2 and less than 5 send a mail to team lead, if more than 5 and less than 9 then send a mail to manager using power automate.

Create approve & reject ribbon buttons, once click on approve it should change the status field to approve.If clicked on reject it should change to Reject. Based on status field change trigger work flow to send a email to stating request is approved/Rejected.

How to get and set values in plugins?