Quick Post:
It is an normal request to get to auto set the Business Process Flow stage based on some condition. It is stright forward and you will also find many blogs to update the stage. But when i was working on it, it found it difficult to search for a post that explains how to get Process ID and Stage ID based on name. This is important to avoid hardcoding of process and stage id in the code.
Below sample will help to get both Process ID and Stage ID based on Process and Stage name and also update the business process stage.
Guid processId = Guid.Empty;
Guid stageId = Guid.Empty;
//Get Process ID for BPF "Case Resolution" EntityCollection workflowRecords = new EntityCollection(); QueryExpression query = new QueryExpression("workflow"); query.NoLock = true; query.ColumnSet = new ColumnSet("workflowid"); query.Criteria.AddCondition("name", ConditionOperator.Equal, "Case Resolution"); workflowRecords = service.RetrieveMultiple(query); if (workflowRecords.Entities.Count > 0) { processId = workflowRecords.Entities.FirstOrDefault().GetAttributeValue<Guid>("workflowid"); } //Get Stage ID for Stage "Resolution" EntityCollection stageRecords = new EntityCollection(); QueryExpression query2 = new QueryExpression("processstage"); query2.NoLock = true; query2.ColumnSet = new ColumnSet("processstageid"); query2.Criteria.AddCondition("stagename", ConditionOperator.Equal, "Resolutions"); query2.Criteria.AddCondition("processid", ConditionOperator.Equal, processId); stageRecords = service.RetrieveMultiple(query2); if (stageRecords.Entities.Count > 0) { stageId = stageRecords.Entities.FirstOrDefault().GetAttributeValue<Guid>("processstageid"); } //Change the stage Entity updatedStage = new Entity("incident"); //Parent entity on which the BPF is updatedStage.Id = new Guid("ED973BF5-D382-493B-8E31-55892B2F0C52"); //Record id for which you which to update stage. updatedStage["stageid"] = stageId; updatedStage["processid"] = processId; service.Update(updatedStage);
Hope this helps!