Posts Tagged Business Process Flows in CRM 2013

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 Comment