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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);



        

    
答案

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

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

另一答案
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);
            
        

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

另一答案

根据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);

以上是关于CRM 2011,以编程方式停止自定义工作流程的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Android 中以编程方式触发自定义信息窗口

已关闭 - 复制自定义工作项以重新分配给其他工作项 Azure DevOps

存储来自自定义工作集的 Web 音频处理数据以转换为 wav 文件

C++ “**.exe已停止工作”是啥原因?

以编程方式启动和停止 IIS Express

从自定义工作项内部参考 Wiki 页面