Programmatically updating stages/process for Business Process Flow in CRM 2013


New release of CRM 2013 allows creating Business Process flows for custom entity as well. It also allows creating multiple processes flows for the same entity.  Different set of users can have different flows based on the business requirement. Even in some scenario’s we may need to shift from one process flow to some other process flow which should be generally done dynamically based on certain conditions.

We can enable Business Process Flows setting during creating custom entity or even we can update this in settings after the entity is created.

EnableSetting

 

Once we enable this 2 fields gets created for this entity.

Attributes

So when we want to change the current process flow or the current stage we need to update these fields. Unfortunately CRM has not provided any UI to update these fields directly. So we can update this either through JavaScript/plugins using CRM API calls.

Let’s see how to get these values and update accordingly. In my sample I have a Business Process Flow called – “Business Process for Enquiry” with the following stages – “New, Contacted, Confirmed, Visa processed and Close”.

BPF

As highlighted above we can enable this flow for distinct/different set of security Roles.

I need to update the stage to CONFIRMED automatically once few fields are updated. To achieve this I wrote the below code in my Plugin.

// Get the Process Id based on the process name
 string procesName = "Business Process for Enquiry";
 var workflow = (from p in orgContext.WorkflowSet
 where p.Name == procesName && p.StateCode == Entities.WorkflowState.Activated
 select p).FirstOrDefault();
 
 if(workflow==null)
 {
 throw new InvalidPluginExecutionException(string.Format("Worflow not found with name {0}",procesName));
 }
 
 //Get the stage id based on the Stage Name
 string stageName = "CONFIRMED";
 var stage = (from p in orgContext.ProcessStageSet
 where p.StageName==stageName.ToUpper() && p.ProcessId.Id==workflow.WorkflowId
 select p).FirstOrDefault();
 if(stage ==null)
 {
 throw new InvalidPluginExecutionException(string.Format("Stage not found with name {0}", stageName));
 }
//Update the record with the new stage
 Entities.new_enquiry updateEnquiry = new Entities.new_enquiry();
 updateEnquiry.Id = context.PrimaryEntityId;
 updateEnquiry.stageid = stage.ProcessStageId;
 updateEnquiry.processid = workflow.WorkflowId;
 service.Update(updateEnquiry);

And that’s it. It does the job for me

FinalStageChanged

Happy Reading !!!!

 

 

Advertisements

, , , , ,

  1. #1 by Aurelia Galjour on March 13, 2014 - 7:31 pm

    I am having trouble with the “stage gating” and workflow processes. We have a screen built with 4 different stages, but are unable to access two of the locked stages. All of the required fields are filled out in the first two stages, but when I click “Next Stage” a pop-up appears and it says “All required field must be filled out.” But all of the required fields ARE filled out. Has anyone had this problem? Please let me know if there is a solution. We cannot move forward if we cant access all of the stages we build.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: