Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Retrieve Business Process Flow Stagename in Dynamics 365

Posted on by 1,846

Hello All,

I have been implementing a workflow and I need to capture the stage name (qualify,develop,propose,close) on opportunity for calculating the amount of time spent on each stage. 

I am unable to capture the stage name. I can't find process stage field in the list of 'When Field Changes' in workflow. 

I have followed the following links but still no luck

https://www.powerobjects.com/2015/01/19/accessing-business-process-name-dynamics-crm/

https://worldofitguy.com/2017/06/23/use-workflow-to-fill-a-field-with-the-stage-name-besides-the-opportunity-entity/

I am using Dynamics 365 version 9.0

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Retrieve Business Process Flow Stagename in Dynamics 365

    HI, in the fifth screenshot, is "Active Stage" a field you created on purpose? Because it doesn't exist in my opportunity...

    Edit : Nevermind, i found my mistake... 

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    Alex Fun Wei Jie 33,626 on at
    RE: Retrieve Business Process Flow Stagename in Dynamics 365

    Hi,

    I've made a same requirements for other members,  you can use it as reference.

    Steps 

    Result

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    Alex Fun Wei Jie 33,626 on at
    RE: Retrieve Business Process Flow Stagename in Dynamics 365

    Hi M N,

    only my opinions, you can think more from the below suggestion.

    I think you need to create a custom entities to store all the related stages from the time the stage is entered and stage is exit and you have to consider what if user move to previous stage, do you still want to record the time?

    you can refer on the below post on how to make use of  Stage Entry and Stage Exit to trigger the workflow.

    alexmscrm.blogspot.my/.../dynamics-365-workflow-within-business.html

  • MNarmeen Profile Picture
    MNarmeen 1,846 on at
    RE: Retrieve Business Process Flow Stagename in Dynamics 365

    Thankyou very much Fun Wei Jie. Your solution solved my problem. However, there is still an issue, I need to create a chart of how much time spent on each stage. How can I capture the time spent on each stage? I tried to get it using ActiveStageStartedOn but it changes the time when I go back to a stage. Any solution you could think of regarding this, will be a great help !

  • Verified answer
    Alex Fun Wei Jie Profile Picture
    Alex Fun Wei Jie 33,626 on at
    RE: Retrieve Business Process Flow Stagename in Dynamics 365

    Hi,

    By the way, if your client is practicing multi Business process flows, then you have use some tricks when user is trying to switch Business process flow. Once business process flow is applied, it will never trigger the workflow although you set it process is applied.

    There is one part of below article shows the behavior of process is applied.

    alexmscrm.blogspot.my/.../dynamics-365-workflow-within-business.html

  • Verified answer
    Alex Fun Wei Jie Profile Picture
    Alex Fun Wei Jie 33,626 on at
    RE: Retrieve Business Process Flow Stagename in Dynamics 365

    Hi,

    82064.10.png

    dont use the above link you shared anymore, because both of fields are going to deprecated. Especially where parellel business process flow kicks in.

    https://blogs.msdn.microsoft.com/crm/2017/07/30/how-to-determine-which-business-process-flow-bpf-instance-is-shown-when-a-record-is-opened-2/

    Read a little bit on Dynamics 365 BPF, it kind of different compare to previous version.

    http://alexmscrm.blogspot.my/2017/10/dynamics-365-business-process-flow-and.html

    With Dynamics 365, every BPFs become an entity.

    So you can try to create a workflow with the business process flow, trigger on stage change.

    1145.3.png

    8507.4.png

    Steps to capture stage name for Dynamics 365

    6305.6.png

    5381.7.png

    7002.8.png

    5810.9.png

    82064.10.png

  • Suggested answer
    Radu Chiribelea Profile Picture
    Radu Chiribelea 6,667 on at
    RE: Retrieve Business Process Flow Stagename in Dynamics 365

    Hi MN,

    Please keep in mind that with the latest changes of the BPF in CRM (whereby you get a new entity created for each Business Process Flow) it is no longer recommended to use the process stage fields on the entity.

    Please see: [View:https://msdn.microsoft.com/en-us/library/dn481586.aspx:750:50]

    "Manipulating process related attributes (such as ProcessId, StageId, and TraversedPath) on entities enabled for business process flows does not guarantee consistency of the business process flow state, and is not a supported scenario. The only exception to this is programmatically modifying the ProcessId attribute while creating an entity record to override the default application of the business process flow to the new record. More information: Apply business process flow while creating an entity record"

    The processes stages are stored in the ProcessStageBase and the process stages part of the BFP Active path can be retrieved via RetrieveActivePathRequest.

    These will be returned in the RetrieveActivePathResponse as part of the ProcessStages properties - https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.retrieveactivepathresponse.aspx

     You can use following code snippet to retrieve the data you are after:

                        // 1.   Retrieve the Active Process Instance together with the ActiveStageId and also the unique name of the workflow
                        //      The Workflow unique name will be needed at a later stage to, when updating the stage 
    
                        RetrieveProcessInstancesRequest retrieveProcessInstancesRequest = new RetrieveProcessInstancesRequest
                        {
                            EntityId = entityId,
                            EntityLogicalName = "yourEntityLogicalName"
                        };
    
                        RetrieveProcessInstancesResponse retrieveProcessInstancesResponse = 
                            (RetrieveProcessInstancesResponse)serviceProxy.Execute(retrieveProcessInstancesRequest);
    
                        Guid activeProcessInstaceId = new Guid(retrieveProcessInstancesResponse.Processes.Entities[0].Id.ToString());
    
                        //if you also want to get the workflow id - you can get it as well
                        EntityReference workflowReference = 
                            (EntityReference)retrieveProcessInstancesResponse.Processes.Entities[0].Attributes["processid"];
    
                        Workflow instanceWorkflow = (Workflow)serviceProxy.Retrieve("workflow", workflowReference.Id, new ColumnSet(true));
    
                        // 2.   Get the Active Path and the available stages that can be switched to in the BPF
                        RetrieveActivePathRequest retrieveActivePathRequest = new RetrieveActivePathRequest
                        {
                            ProcessInstanceId = activeProcessInstaceId
                        };
    
                        RetrieveActivePathResponse retrieveActivePathResponse = 
                            (RetrieveActivePathResponse)serviceProxy.Execute(retrieveActivePathRequest);
    
                        Dictionary<String, Guid> processStages = new Dictionary<String, Guid>(); 
    
                        foreach(var processStage in retrieveActivePathResponse.ProcessStages.Entities)
                        {
                            processStages.Add(processStage["stagename"].ToString(), new Guid(processStage["processstageid"].ToString())); 
                        }


    Hope this helps,

    Radu 

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Complete!

🔔 Be sure to subscribe to the new forums you are interested in to stay up to date! 🔔

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,942 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,389 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans