I am using below mentioned custom work flow code for updating BPF stage automatically. I have bind my custom workflow with CRM standard work flow.
1. Code :
public void UpdateBusinessProcessStage(rmContext dbContext, IOrganizationService service, Account objAccount)
{
// Get the Process Id based on the process name
string procesName = "Create Account From NAV";
var workflow = (from p in dbContext.WorkflowSet
where p.Name == procesName && p.StateCode == 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 = "Qualified Prospect";
var stage = (from p in dbContext.ProcessStageSet
where p.StageName == stageName && p.ProcessId.Id == workflow.WorkflowId
select p).FirstOrDefault();
//foreach (var item in dbContext.ProcessStageSet)
//{
// stageName += item.StageName + "-" + item.Id;
//}
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;
//service.Update(updateEnquiry);
//QueryExpression queryStage = new QueryExpression { EntityName = "processstage" };
//queryStage.ColumnSet = new ColumnSet("stagename", "processid");
//queryStage.Criteria.AddCondition("stagename", ConditionOperator.Equal, stageName);
//queryStage.Criteria.AddCondition("processid", ConditionOperator.Equal, workflow.WorkflowId);
//EntityCollection stages = service.RetrieveMultiple(queryStage);
objAccount.StageId = stage.Id;
objAccount.ProcessId = workflow.WorkflowId;
service.Update(objAccount);
}
This code is not working. Please let me know if any correction in this code.
2. I got stage name and directly assigned to Entity stageid but it didn't work for me.
Please do needful if any one have solution for this issue.
*This post is locked for comments