3

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"

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jan
  • 127
  • 1
  • 10

2 Answers2

3

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.

Community
  • 1
  • 1
Dave Clark
  • 2,243
  • 15
  • 32
2

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.

jasonscript
  • 6,039
  • 3
  • 28
  • 43
Dave Clark
  • 2,243
  • 15
  • 32
  • 2
    Thanks Dave. My bad - I have not explicitally written that I would like to do it using C# (Custom Action). I've editted the question. Still the solution you suggest could be used in my case as workaround. – Jan Apr 11 '17 at 13:46
  • 2
    The anwser is almost correct. It seams that there is a mistake in the Documentation. Working call: Xrm.Page.data.process.setStatus("finished"); – Jan Apr 12 '17 at 08:59
  • 1
    In Doc there is also a status "abandoned" which real name is "aborted" – Jan Apr 12 '17 at 09:05
  • You're right. I've just tested this myself on Dynamics 365 (8.2.0.798): `finish` did not work whereas `finished` did work! – Dave Clark Apr 12 '17 at 09:20