Search code examples
dynamics-crmmicrosoft-dynamicsdynamics-crm-webapi

Dynamics 365 - Web API Change the Active Business Process Flow on a record


Hi I am trying to switch the active business process on a record. Basically, I have two business process flows and I want to move a record (lead) to the second business process flow, it is currently on the first business process flow.

I have tried utilizing the SetProcess Action. The request is successful (204) but it is not updating the Business Process Flow on the record to the correct one.

Here is the request I am sending:

url = "[Org Url]SetProcess"
data = data_action = {
    'Target': f'leads({leadid})',
    'NewProcess': 'workflows(9e5b9ac9-2e14-4293-bc22-076e8accb444)',
}

But when I refresh the record in Dynamics it still shows the old business process flow. The workflow id is the correct business process flow.

Any help is appreciated!


Solution

  • From UI perspective - when you want to Abort an existing BPF, switch to a new BPF altogether, it's a one or two clicks job and platform does everything for you in background.

    Whereas from SDK/API perspective - we have to take care of few things. It used to be attributes in same CRM record (ex. account, lead) storing the process and stage Ids of associated BPF. Later it became a N:N table storing process and stage Id for any active BPF and this is to entertain the use cases like - different teams can have different BPF on same CRM record (ex. account or lead) and BPF can be abandoned and can start over with a new BPF.

    Now when you wanted to start a new BPF instance and abandon the old BPF instance, its not completely transacted when you just create a new instance of new BPF, so its needed to remove the old entries in BPF table. Then the new instances were taking effect once it is created and navigated to that form.

    You can do these through web api calls. Some sample payloads below:

    Use the following request to create an instance of your business process flow definition for an account row

    POST [Organization URI]/api/data/v9.0/new_mycustombpfs HTTP/1.1 
    Content-Type: application/json; charset=utf-8 
    OData-MaxVersion: 4.0 
    OData-Version: 4.0 
    Accept: application/json 
    
    {
        "[email protected]": "/accounts(a176be9e-9a68-e711-80e7-00155d41e206)",
        "[email protected]": "/processstages(9a9185f5-b75b-4bbb-9c2b-a6626683b99b)"    
    }
    

    Delete a business process flow table row (process instance)

    DELETE [Organization URI]/api/data/v9.0/new_mycustombpfs(dc2ab599-306d-e811-80ff-00155d513100) HTTP/1.1
    

    Reference