We all know the tried and true method of progressing a business process flow using JavaScript but sometimes that just won’t cut it, you just need to have a plugin. This could be because you want to progress the BPF after an asynchronous plugin has returned a result or for any number of other reasons.
We’ve all seen this achieved through workflows but maybe you don’t want that added step of calling/triggering a workflow or to have to determine and hard code which is your next step in your BPF. Well, it turns out there is a way to get your BPF moving using a plugin.
The first thing you are going to want to do is find the Guid of your BPF instance and retrieve the active path.
RetrieveActivePathRequest pathReq = new RetrieveActivePathRequest
{
ProcessInstanceId = <THE INSTANCE OF YOUR BPF>
};
RetrieveActivePathResponse pathResp = (RetrieveActivePathResponse)sdk.Execute(pathReq);
Like the name implies this request will retrieve the current active BPF path for your record.
From here you get information like the current active stage and the list of all the stages in the bpf:
Guid activeStage = bpf.Get<Guid>("activestageid");
EntityCollection stages = (EntityCollection)pathResp.Results.Values.ToList().FirstOrDefault();
Then find the index of the current active stage in the paths stages.
Entity stage = stages.Entities.Where(c => c.Id == activeStage)FirstOrDefault();
int index = stages.Entities.IndexOf(stage);
Entity stage2 = stages[index++];
To move to the next stage you simply grab the next stage (i.e. index+1) and set that as the active stage.
Remember to update the traversed path.
bpf["activestageid"] = stage2.ToEntityReference();
bpf["traversedpath"] = traversedpath;
sdk.Update(bpf);
If you are wanting to skip a stage remember to still add it to the traversed path otherwise you will not see it in the BPF on the entity form. I hope this will be helpful to you as much as it will be to me when I reference this blog in the future.