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

Summary: Programmatically updating stages/process for Business Process Flow in MS 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.
Plugins / WorkFlows:
// 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);
Reference Link: http://deepakexploring.wordpress.com/2013/11/21/programmatically-updating-stagesprocess-for-business-Aprocess-flow-in-crm-2013/


0 comments:

Post a Comment

Popular Posts