How to Create a Business Process Flow and Get active stage of record and Update the Stage using C# in D365:
- Create a Business Process Flow
Ex: apy_businessprocessflow
- On Create of Contact record Create a business process flow and Get active stage and Set the Stage
- Register Plugin on Create of Contact Entity
using System;
namespace APY.Plugins.Sales
{
public class
ManageContact : IPlugin
{
public void
Execute(IServiceProvider serviceProvider)
{
ITracingService tracer =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(null);
if
(context.MessageName == "Create" && context.StageType() ==
ExecutionContextExtensions.StageTypeValues.PostExecution)
{
Entity
contactTarget = (Entity)context.InputParameters["Target"];
if
(contactTarget.LogicalName != "contact")
{
return;
}
BusinessProcessFlow businessProcessFlow = new
BusinessProcessFlow(contactTarget, service);
businessProcessFlow.CreateBPFInstanceForTheContact();
}
}
}
}
Below method is to create a BPF record
public void
CreateBPFInstanceForTheContact()
{
Entity
activeProcessInstance = GetActiveBPFDetails();
if
(activeProcessInstance == null)
{
var
patientFlowRecord = new Entity("apy_businessprocessflow");
patientFlowRecord.Attributes["bpf_contactid"] =
Record.ToEntityReference();
Service.Create(patientFlowRecord);
}
}
Get the active BPF stage details
public Entity GetActiveBPFDetails()
{
Entity
activeProcessInstance = null;
RetrieveProcessInstancesRequest entityBPFsRequest = new
RetrieveProcessInstancesRequest
{
EntityId = Record.Id,
EntityLogicalName = Record.LogicalName
};
RetrieveProcessInstancesResponse entityBPFsResponse = (RetrieveProcessInstancesResponse)Service.Execute(entityBPFsRequest);
int currentStagePosition = -1;
RetrieveActivePathResponse pathResp =
businessProcessFlow.GetAllStagesOfSelectedBPF(activeBPFId, activeStageId, ref
currentStagePosition);
Entity
contact = Service.Retrieve(Record.LogicalName, Record.Id, new ColumnSet("fullname",
"firstname", "lastname"));
Dictionary<int,
string> stageKeyValue = new Dictionary<int, string>();
for (int i = 0; i <
pathResp.ProcessStages.Entities.Count; i++)
{
stageKeyValue.Add(i,
pathResp.ProcessStages.Entities[i].Attributes["stagename"].ToString());
}
if (entityBPFsResponse.Processes
!= null && entityBPFsResponse.Processes.Entities.Count > 0)
{
activeProcessInstance = entityBPFsResponse.Processes.Entities[0];
}
else
if (hasScheduledImplantDate && currentStagePosition <=
GetStagePosition(stageKeyValue, ActivationStage))
{
businessProcessFlow.UpdateProcessStage(GetStagePosition(stageKeyValue,
FineTuneStage), processFlowName, activeBPFId, pathResp);
}
return
activeProcessInstance;
}
Get stage Position
public int GetStagePosition(Dictionary<int, string>
stageKeyValue, string stageName)
{
return
stageKeyValue.Where(x => x.Value == stageName).Select(y =>
y.Key).FirstOrDefault();
}
Get all the stage of selected BPF
public
RetrieveActivePathResponse GetAllStagesOfSelectedBPF(Guid activeBPFId, Guid
activeStageId, ref int currentStagePosition)
{
RetrieveActivePathRequest pathReq = new RetrieveActivePathRequest
{
ProcessInstanceId = activeBPFId
};
RetrieveActivePathResponse pathResp = (RetrieveActivePathResponse)Service.Execute(pathReq);
for (int i
= 0; i < pathResp.ProcessStages.Entities.Count; i++)
{
if
(pathResp.ProcessStages.Entities[i].Attributes["processstageid"].ToString()
== activeStageId.ToString())
{
currentStagePosition = i;
}
}
return
pathResp;
}
- Unit Test case for Business process flow
Comments
Post a Comment