How to trigger the plugin on any Business Process Flow stage change and update custom entity record related to the Active Stage

Introduction

Recently while working on a customer requirement, we came across a scenario where we needed to trigger a plugin on change of Business Process Flow stages. So, in the pursuit of finding a solution, we came across workflow and process stage entities, but they were not very useful in this scenario. Our requirement was to trigger the plugin on the Business Process Flow stage change of an Employee entity. So, we tried writing a plugin for the update of an Employee entity. There’s one field “stageid” on every entity on which a Business process flow is configured. We thought on the change of the Business Process Flow stage this field might be getting updated with the Id of the current active stage, which we can further use to fulfill our requirement. But to our disappointment, this field is now deprecated and thus we are left empty-handed.

Further investigation brought light to the reality i.e., for every Business Process Flow, a subsequent Entity gets created in Dynamic365 CRM. In this blog, we shall further see how using this entity, we achieved the customer’s requirement.

Scenario:

To handle the recruitment process there’s an Employee table as well as an Employee Stages table. We want to update the status of the related Employee Stage record for whichever stage currently is active.

In the below Image, we can see that even when we changed the stage of the Employee record the stage id stays blank. In the name itself, we can see it is stated that it’s now deprecated.

Solution:

The below image represents the Business Process Flow on the Employee Entity to handle the employee recruitment process.

Business Process Flow stage

Business Process Flow stage

In the above screenshot, we can see that CRM created an entity for the custom Business Process Flow, and it contains the “activestageid” field which has the current active stage for a particular record.

Now we will write a plugin on the update of the above Business Process Flow entity and add the filtering attribute of “activestageid”.

Business Process Flow stageIn the below image, we find the first record of the Employee Stages entity having the status as Active, and the remaining records are currently in an Inactive state.

Business Process Flow stage

When we change the stage of the Business Process Flow from Sourcing to Screening, using our plugin we are changing the status of the related Employee Stages entity record named Screening to Active.

Business Process Flow stage

Code:

private void changeBPFstage(PluginConfig config, StringBuilder traceLogBuilder, Entity targetEntity)

{

//Get current BPF activestageID

EntityReference currentBpfStageId = targetEntity.Attributes.Contains(“activestageid”) ? targetEntity.GetAttributeValue<EntityReference>(“activestageid”) : null;

//Execute Fetchxml

string fetchXML = “< fetch >” +

” <entity name=”new_employeestage”>” +

” <attribute name=”new_ employeestageid”/>” +

” <filter type=”and”>” +

” <condition attribute=”new_ employeestageid” operator=”eq” value=””+currentBpfStageId+””/>” +

” </ filter>” +

“</ entity>” +

” </fetch>”;

 

EntityCollection entityCollection = config.Service.RetrieveMultiple(new FetchExpression(fetchXML));

//To check entitycollection count is greater than 0

if(entityCollection.Entities.Count>0)

{

//Update status of employee record

Entity entemployee = new Entity(“new_employeestage”, targetEntity.Id);

entemployee.Attributes[“statuscode”] = new OptionSetValue(1);

entemployee.Attributes[“statecode”] = new OptionSetValue(0);

config.Service.Update(entemployee);

}

}

Conclusion

In this way, we can easily call the plugin on the Business Process Flow stage change.

Business Process Checklist (2)

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *