3
votes

I am managing the whole CRM process flow in C# code, using the newest SDK version (8.2). Moving forward works fine, simply by updating the stageid on the relevant entity. However I have no idea, how could I finish the last stage = how could I set process to finished. I'd like to call exactly the same actions as with clicking the button "set finished"

2

2 Answers

2
votes

To set the current status of the active process instance to finished use:

Xrm.Page.data.process.setStatus("finished");

From the Microsoft documentation:

Xrm.Page.data.process.setStatus(status, callbackFunction);

status is a string that can be active, abandoned or finish. callbackFunction is an optional function to call when the operation is complete.

Note: though the documentation says to use finish this does not work: use finished.

3
votes

When you create a business process flow you create a custom entity. For example, if you create a business process flow called Marketing Management in the default solution, you will create an entity called new_marketingmanagement.

When you activate a business process flow on a record, an instance of that entity is created. The instance holds information such as which stage the process is at and when the process was started.

You can deactivate the instance using Microsoft.Xrm.Sdk.Messages.SetStateRequest:

var stateRequest = new SetStateRequest 
{
    EntityMoniker = new EntityReference(processFlowName, processId),
    State = new OptionSetValue(1), // Inactive.
    Status = new OptionSetValue(2) // Finished.
};
service.Execute(stateRequest);

Where processFlowName is the logical name of your business process flow as a string and processId is the ID of the process flow instance you want to deactivate as a GUID.

To find the ID of your process flow instance, you can query the attribute _bpf_<primary_key>_value where <primary_key> should be replaced with the primary key of the entity which your process is on. For example on the Account entity this would read _bpf_accountid_value.