This is a simple code example that you can use to do move stages of a Business Process Flow (it can be improved, for example, to not have the stages id in the code). Also, I was using early bound class for this example. The field TraversedPath needs to be updated accordingly the stage that you are moving, so if you are going forward you need to add the stages, if you are going backward you need to remove the stages.
I have 3 Opportunities in the system:
- Opp1 -> Stage1
- Opp2 -> Stage2
- Opp3 -> Stage3
And want to change the stages to:
- Opp1 -> Stage3
- Opp2 -> Stage1
- Opp3 -> Stage2
try
{
string connectionString = GetServiceConfiguration();
CrmServiceClient conn = new CrmServiceClient(connectionString);
_serviceProxy = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;
string fetchXml = @"";
EntityCollection result = _serviceProxy.RetrieveMultiple(new FetchExpression(fetchXml));
string processid = "3E8EBEE6-A2BC-4451-9C5F-B146B085413A";
string stage1 = "6b9ce798-221a-4260-90b2-2a95ed51a5bc";
string stage2 = "650e06b4-789b-46c1-822b-0da76bedb1ed";
string stage3 = "d3ca8878-8d7b-47b9-852d-fcd838790cfd";
foreach (Opportunity opp in result.Entities)
{
string id = opp.Id.ToString();
Entity op = new Entity("opportunity");
op.Id = opp.Id;
if (opp.StageId.Value.ToString().ToUpper() == stage1.ToUpper())
{
op["processid"] = new Guid(processid);
op["stageid"] = new Guid(stage3);
op["traversedpath"] = new Guid(stage1.ToLower()) + "," + new Guid(stage2.ToLower()) + "," + new Guid(stage3.ToLower()).ToString();
}
if (opp.StageId.Value.ToString().ToUpper() == stage2.ToUpper())
{
op["processid"] = new Guid(processid);
op["stageid"] = new Guid(stage1);
op["traversedpath"] = new Guid(stage1.ToLower()).ToString();
}
if (opp.StageId.Value.ToString().ToUpper() == stage3.ToUpper())
{
op["processid"] = new Guid(processid);
op["stageid"] = new Guid(stage2);
op["traversedpath"] = new Guid(stage1.ToLower()) + "," + new Guid(stage2.ToLower()).ToString();
}
_serviceProxy.Update(op);
}
}
catch (Exception ex)
{
throw;
}