CRM 2011,以编程方式停止自定义工作流程

时间:2012-07-05 09:11:46

标签: workflow dynamics-crm-2011

我一直试图以编程方式停止工作流程。 我已经在各种帖子和msdn中阅读过这可以通过更新来完成 通过更新请求的Asyncoperation状态。 但是,每次我更新请求。工作流程停留在中间阶段,例如取消或暂停,并且不会达到最终状态。

任何想法?

protected void ExecutePostAccountUpdate(LocalPluginContext localContext)
    {
        if (localContext == null)
        {
            throw new ArgumentNullException("localContext");
        }

        string fetchXML = "<fetch mapping='logical' count='50' version='1.0'>" +
                          "<entity name='asyncoperation'>" +
                          "<filter>" +
                          "<condition attribute='regardingobjectid' operator='eq' value='" +
                          localContext.PluginExecutionContext.PrimaryEntityId + "' />" +
                          "</filter>" +
                          "</entity>" +
                          "</fetch>";
        EntityCollection col = localContext.OrganizationService.RetrieveMultiple(new FetchExpression(fetchXML));
        if (col.Entities.Count > 0)
        {
            AsyncOperation a = (AsyncOperation)col[0];
            a.StateCode = AsyncOperationState.Completed;
            a.StatusCode = new OptionSetValue(32);
            localContext.OrganizationService.Update(a);



        }

    }

3 个答案:

答案 0 :(得分:1)

查看我的博客:How to Cancel Workflow Programmatically using C#

确保用户具有取消系统作业的权限。

答案 1 :(得分:1)

QueryExpression queryExpression = new QueryExpression("asyncoperation") { ColumnSet = new ColumnSet("statuscode") };
        queryExpression.Criteria.AddCondition("name", ConditionOperator.Equal, Name of Workflow);
        queryExpression.Criteria.AddCondition("regardingobjectid", ConditionOperator.Equal, regardingobjectId);
        var asyncOperations = organizationService.RetrieveMultiple(queryExpression);

        foreach (var asyncOperation in asyncOperations.Entities)
        {
            if (((OptionSetValue)asyncOperation["statuscode"]).Value == 10 || // Waiting
                ((OptionSetValue)asyncOperation["statuscode"]).Value == 20 || // In Process
                ((OptionSetValue)asyncOperation["statuscode"]).Value == 0)
            {
                Entity operation = new Entity("asyncoperation")
                {
                    Id = asyncOperation.Id,
                    ["statecode"] = new OptionSetValue(3),
                    ["statuscode"] = new OptionSetValue(32)
                };

                organizationService.Update(operation);
            }
        }

确保用户有权取消系统作业。

答案 2 :(得分:0)

根据this post,您似乎可以通过代码取消发布工作流程。

注意:这不一定会停止正在进行的工作流程,但会阻止启动该类型的任何新工作流程。

const int WorkflowStatusDraft = 1;
const int WorkflowStatusPublished = 2;

public void PublishWorkflow(Guid workflowId)
{
    SetStateWorkflowRequest publishRequest = new SetStateWorkflowRequest();
    publishRequest.EntityId = workflowId;
    publishRequest.WorkflowState = WorkflowState.Published;
    publishRequest.WorkflowStatus = WorkflowStatusPublished;

    this.CrmService.Execute(publishRequest);
}

public void UnpublishWorkflow(Guid workflowId)
{
    SetStateWorkflowRequest unpublishRequest = new SetStateWorkflowRequest();
    unpublishRequest.EntityId = workflowId;
    unpublishRequest.WorkflowState = WorkflowState.Draft;
    unpublishRequest.WorkflowStatus = WorkflowStatusDraft;

    this.CrmService.Execute(unpublishRequest);
}