Plugin can be written to dynamicaly change BPF ( bussiness process flow ) of an entity acc to diffrent condition or Configrations ,  It requires Two main Id

1. ProcessID : to get workflowID
2. StageID Of that process.

Below is the code to change bpf of an entity using plugin :

 QueryExpression queryworkflow = new QueryExpression(Workflow.EntityLogicalName);
            queryworkflow.ColumnSet = new ColumnSet(true);
          ConditionExpression cond1 =  new ConditionExpression("workflowid", ConditionOperator.Equal, processId);
         ConditionExpression cond2 =   new ConditionExpression("statecode", ConditionOperator.Equal, 1);
         FilterExpression filter = new FilterExpression();

         queryworkflow .Criteria.AddFilter(filter);
         EntityCollection workflowcoll = service.RetrieveMultiple(queryworkflow);

// get default stage id of that process

            QueryExpression querystage = new QueryExpression("processstage");
            querystage.ColumnSet = new ColumnSet(true);
            ConditionExpression cond1 = new ConditionExpression("stagecategory", ConditionOperator.Equal, 0);
            ConditionExpression cond2 = new ConditionExpression("processid", ConditionOperator.Equal, workflowcoll.Entities[0].Id);
            FilterExpression filter1 = new FilterExpression();

            EntityCollection stagecoll = service.RetrieveMultiple(querystage);

            // update the fields in entity to change the BPF 

             Entity providedentity = new Entity(entity.LogicalName);
            providedentity.Id = entity.Id;
                providedentity["processid"] = workflowcoll.Entities[0].Id;
                providedentity["stageid"] = stagecoll.Entities[0].Id;

Thats all Entity BPF Will be auto assigned as given in Code .

Hope this Helpled you , for any query or suggestion please coment below , thanks

SOURCE : JUST2CODE.IN Subscribe to our YouTube channel : https://www.youtube.com/user/TheRussell2012