背景:通过WebService、WebAPI等集成方式,提交单据时,希望自动触发工作流。
提交操作本身仅仅处理单据状态切换,并不触发工作流,要自动触发工作流,必须要通过空操作 + 空操作的操作插件实现。
如下示例代码,用于演示如何自动启动工作流(未测试),如果单据未配置工作流模板,则走普通提交处理:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Kingdee.BOS;
using Kingdee.BOS.Util;
using Kingdee.BOS.Orm;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.App;
// Kingdee.BOS.Workflow.Contracts.dll
using Kingdee.BOS.Workflow.Contracts;
// Kingdee.BOS.Workflow.Models.dll
using Kingdee.BOS.Workflow.Models.Template;
using Kingdee.BOS.Workflow.Models.EnumStatus;
namespace JDSample.ServicePlugIn.Operation
{
///
/// 演示在单据的空操作(DoNothing)上,自动提交工作流
///
[Description("演示自动提交工作流")]
public class S150724AutoSubmitWFPlug : AbstractOperationServicePlugIn
{
///
/// 操作完毕,提交事务之前
///
///
///
/// 通常在此事件中进行数据同步
///
public override void EndOperationTransaction(EndOperationTransactionArgs e)
{
// 要求操作结束后,显示提示信息
this.OperationResult.IsShowMessage = true;
// 本演示只提交一张单据,批量处理,请自行修改代码实现
string formId = this.BusinessInfo.GetForm().Id;
string billId = Convert.ToString(e.DataEntitys[0][0]);
// 首先判断单据是否已经有未完成的工作流
IProcInstService procInstService = Kingdee.BOS.Workflow.Contracts.ServiceFactory.GetProcInstService(this.Context);
bool isExist = procInstService.CheckUnCompletePrcInstExsit(this.Context, formId, billId);
if (isExist == true)
{
throw new KDBusinessException("AutoSubmit-001", "该单据已经启动了流程,不允许重复提交!");
}
// 读取单据的工作流配置模板
IWorkflowTemplateService wfTemplateService = Kingdee.BOS.Workflow.Contracts.ServiceFactory.GetWorkflowTemplateService(this.Context);
List
formId, new string[] { billId }, this.Context);
if (findProcResultList == null || findProcResultList.Count == 0)
{
throw new KDBusinessException("AutoSubmit-002", "查找单据适用的流程模板失败,不允许提交工作流!");
}
// 设置提交参数:忽略操作过程中的警告,避免与用户交互
OperateOption submitOption = OperateOption.Create();
submitOption.SetIgnoreWarning(true);
IOperationResult submitResult = null;
FindPrcResult findProcResult = findProcResultList[0];
if (findProcResult.Result == TemplateResultType.Error)
{
throw new KDBusinessException("AutoSubmit-003", "单据不符合流程启动条件,不允许提交工作流!");
}
else if (findProcResult.Result != TemplateResultType.Normal)
{// 本单无适用的流程图,直接走传统审批
ISubmitService submitService = ServiceHelper.GetService
submitResult = submitService.Submit(this.Context, this.BusinessInfo,
new object[] { billId }, "Submit", submitOption);
}
else
{// 走工作流
IBOSWorkflowService wfService = Kingdee.BOS.Workflow.Contracts.ServiceFactory.GetBOSWorkflowService(this.Context);
submitResult = wfService.ListSubmit(this.Context, this.BusinessInfo,
0, new object[] { billId }, findProcResultList, submitOption);
}
// 判断提交结果
if (submitResult.IsSuccess == true)
{
// 自动提交成功,显示空操作本身的成功提示即可
}
else
{
submitResult.MergeValidateErrors();
if (submitResult.OperateResult == null)
{// 未知原因导致提交失败
throw new KDBusinessException("AutoSubmit-004", "未知原因导致自动提交失败!");
}
else
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("自动提交失败,失败原因:");
foreach (var operateResult in submitResult.OperateResult)
{
sb.AppendLine(operateResult.Message);
}
throw new KDBusinessException("AutoSubmit-005", sb.ToString());
}
}
}
}
}
推荐阅读