Activating BPF stage using plugin based on the option set Present in the Form

 Activating BPF stage using plugin

For Each stage, Guid is available. we need to retrieve the each stage guid. 

For retriving the guid we need to prepare the one url for the bpf.

Step1: Developer resource url/processstages$select=stagename&$filter=processid/workflowid eq BPF Guid. 

After search, it will show like below and copy the all stage guids for further use.

Step2: Prepare on bpf for the entity with adding some fields to bpf.

Step3: Add the option set to from like below

Step2: Prepare one class library project and add the necessary reference to project.

After adding, follow the below code


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 Week4Plugins

{

    public class Employbpfactivation : 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);

            try

            {

                if (context.Depth == 1)

                {

                    string logicalNameOfBPF = "new_stagechangeckecing";  //new_stagechangeckecing


                    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("crc78_stage") ? (OptionSetValue)eMployOobject["crc78_stage"] : 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 == 173090000)

                        {

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

                            entBPF["activestageid"] = new EntityReference("processstage", new Guid("6fe84b53-7d26-4c1f-b5a3-f45875dade28"));

                        }

                        else if (Stagestatus.Value == 173090001)

                        {

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

                            entBPF["activestageid"] = new EntityReference("processstage", new Guid("f301af6a-d152-4364-a790-00d1d620e212"));

                        }

                        else if (Stagestatus.Value == 173090002)

                        {


                            entBPF["activestageid"] = new EntityReference("processstage", new Guid("a260f09b-d3b0-4d57-9838-c027cfc9851d"));

                        }

                        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;

        }

    }

 }

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


Below option set is in developing, then bpf also in developing stage.


By following these steps we can achieve the Stage activation using plugin


Thnks for reading my post. Comment and follow the page for latest updates.






Comments

Popular posts from this blog

Connect MS Crm To Postman For Webapi Calls.

create HTML page show all contact related to specific account, add this webresource as a subgrid in the form

create one PCF control and embed into crm