web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Retrieve Business Process Flow Stagename in Dynamics 365

(0) ShareShare
ReportReport
Posted on by 1,848

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

I have the same question (0)
  • Suggested answer
    Radu Chiribelea Profile Picture
    6,667 on at

    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 

  • Verified answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    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

  • Verified answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    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

  • MNarmeen Profile Picture
    1,848 on at

    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 !

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    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

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

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

    Steps 

    Result

  • Community Member Profile Picture
    on at

    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... 

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans