Posted on 20. September 2013

How to change process and stage programmatically

Scenario

UPDATE: With CRM2015 - the new client side process API should be used to set the current step. The following approach should be only used for setting the process to the first step.

If you have more than one Dynamics CRM 2013 Business Process Flow for a given entity users can easily change the current process by using the 'Switch Process' Command Bar button:

Once changed, you can identify which process is running by hovering over the 'I' on the bottom right of the process flow:

If your users would like to change the Business Process Flow stage dynamically based on a given set of attribute values then it is not immediately obvious how this can be achieved. This post provides one solution using a custom workflow activity in combination with the new Synchronous Workflow feature of Dynamics CRM 2013.

Solution

The currently selected process stage on a given entity record is stored in CRM using the following attributes:

  • processid – GUID of the selected Business Process (workflow)
  • stageid – GUID of the selected Process Stage (processstage)

Initially I thought that these attributes might be able to be updated using a workflow directly, but it turns out you can only update them via code.

You will need to know how to write Custom Workflow Activities and add JavaScript Web Resources to follow these steps:

1. Using the Visual Studio Developer Tookit found in the SDK, create a new Custom Workflow Activity that accepts the following input arguments:

[RequiredArgument]
[Input("Process Name")]
public InArgument Process { get; set; }

[RequiredArgument]
[Input("Process Stage Name")]
public InArgument ProcessStage { get; set; }

These parameters need to be strings since you cannot pass in EntityReferences to the Workflow and ProcessStage entities.

2. Add to the Execute code the following:

using (var ctx = new OrganizationServiceContext(service))
{	
    // Get the processid using the name provided
    var process = (from p in ctx.CreateQuery()
                   where
                   p.Name == Process.Get(executionContext)
                   &&
                   p.StateCode == WorkflowState.Activated
                   select new Workflow
                   {
                       WorkflowId = p.WorkflowId

                   }).FirstOrDefault();
    if (process==null)
        throw new InvalidPluginExecutionException(String.Format("Process '{0}' not found",Process.Get(executionContext)));

    // Get the stage id using the name provided
    var stage = (from s in ctx.CreateQuery()
                 where
                 s.StageName == ProcessStage.Get(executionContext)
                 &&
                 s.ProcessId.Id == process.WorkflowId
                 select new ProcessStage
                 {
                     ProcessStageId = s.ProcessStageId

                 }).FirstOrDefault();
    if (stage == null)
        throw new InvalidPluginExecutionException(String.Format("Stage '{0}' not found", Process.Get(executionContext)));

    // Change the stage
    Entity updatedStage = new Entity(context.PrimaryEntityName);
    updatedStage.Id = context.PrimaryEntityId;
    updatedStage["stageid"] = stage.ProcessStageId;
    updatedStage["processid"] = process.WorkflowId;
    service.Update(updatedStage);
}

3. Deploy your custom workflow activity.

4. Create a New Workflow that is defined with the following parameters:

  • Activate As: Process
  • Run this workflow in the background : NO
  • Scope: Organization (if you want it to run for all users)
  • Start when: Before – Record fields change. Select the attributes that the process stage is dependant on so that it does not run on every change.

4. Add a set of conditions to determine if the stage should be changed and call your custom workflow activity providing the Process Name and Process Stage to change to.

5. Since the Process and Stage will not automatically change on the form, you need to add some JavaScript to refresh the form by saving it when the dependant fields are changed. You can do this by adding an onChange event to the dependant fields that calls:

Xrm.Page.data.entity.save()

UPDATE: Rob Boyers pointed out that this doesn't reliably refresh the process, however there isn't currently a supported way of doing it.
You could use the following unsupported method call to reload the whole form:

Mscrm.ReadFormUtilities.openInSameFrame(window._etc, Xrm.Page.data.entity.getId())


Notes:

  1. This technique could also be written using a plugin - but the advantage of using a Synchronous workflow is that the logic that determines the stage can be easily changed without re-compiling code.
  2. You can use the same technique to read the process and stage name so that you can make a decision using the current position in the process. I've included a sample of this too in the download
  3. You can download the sample code from here - http://code.msdn.microsoft.com/Change-Dynamics-CRM-2013-a6beb85e

Comments

pingback niiranen.eu
6:24 PM on Saturday, October 26, 2013

Pingback from niiranen.eu

Getting Your Head Around Dynamics CRM 2013 Processes | Surviving CRM

Surviving CRM Surviving CRM
7:31 PM on Saturday, October 26, 2013

Getting Your Head Around Dynamics CRM 2013 Processes – Part 1

Workflows have always been a central part of the Dynamics CRM product. The typical need for having workflow

Dynamics CRM Pros Dynamics CRM Pros
6:23 PM on Monday, March 03, 2014

Working with Business Process Flows in Microsoft Dynamics CRM 2013

One of the exciting new features of Microsoft Dynamics CRM 2013 is Business Process Flows. This feature

pingback adisys.wordpress.com
4:59 AM on Tuesday, May 27, 2014

Pingback from adisys.wordpress.com

CRM 2013 – Business Process Flow – How to skip the stage across multiple entities dynamically? | ADISYS Team Blog

pingback crmbusiness.wordpress.com
1:21 PM on Friday, June 27, 2014

Pingback from crmbusiness.wordpress.com

Hosk’s Top CRM 2013 articles of the week 27th June 2014 | Hosk's Dynamic CRM Blog

Customer Effective Blog Customer Effective Blog
2:02 PM on Friday, June 27, 2014

Microsoft Dynamics CRM Business Process Flow Lessons Learned

I’ve had the pleasure of completing multiple upgrades to Microsoft Dynamics CRM 2013, and one of the

Customer Effective Blog Customer Effective Blog
3:52 PM on Wednesday, July 09, 2014

Microsoft Dynamics CRM Business Process Flow Lessons Learned

I’ve had the pleasure of completing multiple upgrades to Microsoft Dynamics CRM 2013, and one of the

Customer Effective Blog Customer Effective Blog
3:43 AM on Saturday, July 12, 2014

Microsoft Dynamics CRM Business Process Flow Lessons Learned

I’ve had the pleasure of completing multiple upgrades to Microsoft Dynamics CRM 2013, and one of the

Miss Dynamics CRM's  360° Exploration - 100% Microsoft Technology Miss Dynamics CRM's 360° Exploration - 100% Microsoft Technology
5:46 PM on Thursday, July 24, 2014

Show and Hide Business Process Flow in CRM 2011/2013

Business Process Flow is a great feature in CRM 2013. But, sometimes, in some condition or simple situation

pingback crmsoftwareblog.com
3:50 AM on Sunday, August 31, 2014

Pingback from crmsoftwareblog.com

Dynamics CRM Cross-Entity Business Process Flow - CRM Software Blog

crming.wordpress.com crming.wordpress.com
3:14 PM on Tuesday, March 31, 2015

Pingback from crming.wordpress.com

Customize Business Process Flows using JavaScript | crming

pingback imgroup.com
4:10 PM on Wednesday, April 08, 2015

Pingback from imgroup.com

Customize Business Process Flows using JavaScript - Information Management Group Ltd.

pingback canada.hitachi-solutions.com
6:01 PM on Tuesday, July 21, 2015

Pingback from canada.hitachi-solutions.com

Dynamics CRM Cross-Entity Business Process Flow - Hitachi Solutions Canada

mshahzadg.wordpress.com mshahzadg.wordpress.com
12:01 PM on Monday, October 12, 2015

Pingback from mshahzadg.wordpress.com

Thinking Enterprise Solutions

pingback roids.biz
1:26 PM on Friday, August 05, 2016

Pingback from roids.biz

clenotabs

pingback steroidsforsale.biz
6:32 PM on Wednesday, November 30, 2016

Pingback from steroidsforsale.biz

sustanon

pingback steroidsforsale.biz
8:58 AM on Wednesday, December 21, 2016

Pingback from steroidsforsale.biz

anabolics online

hitachius.wpengine.com hitachius.wpengine.com
4:34 PM on Wednesday, February 01, 2017

Pingback from hitachius.wpengine.com

Dynamics CRM Cross-Entity Business Process Flow - Hitachi Solutions America

pingback intergr8it.net
4:37 PM on Thursday, February 16, 2017

Pingback from intergr8it.net

Intergr8 IT [Richard Barclay]» Blog Archive » CRM 2013 – Update the Business Process and Stage dynamically

pingback crmjames.be
2:09 PM on Thursday, March 16, 2017

Pingback from crmjames.be

CRM Business Process Flows in Dynamics 365 - crmJames

pingback roids.online
12:47 PM on Friday, September 01, 2017

Pingback from roids.online

bitcoin steroids

Comments are closed