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

workflow dynamics-crm-2011

2417 观看

3回复

16 作者的声誉

我一直试图以编程方式停止工作流程。我已经在各种帖子和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);



        }

    }
作者: Eranser 的来源 发布者: 2012 年 7 月 5 日

回应 (3)


0

604 作者的声誉

根据这篇文章,您似乎可以通过代码取消发布工作流程。

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

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);
}
作者: Alec 发布者: 09.07.2012 11:40

1

3405 作者的声誉

看看我的博客:如何使用C#以编程方式取消工作流程

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

作者: Scorpion 发布者: 13.05.2014 02:46

1

11 作者的声誉

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);
            }
        }

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

作者: Saeed DN 发布者: 26.04.2019 11:09
32x32