#使用技巧# K3Cloud BOS 设计 Python & C# 单据转换插件 自动提交审核

栏目:云星空知识作者:金蝶来源:金蝶云社区发布:2024-09-16浏览:1

#使用技巧# K3Cloud BOS 设计 Python & C# 单据转换插件 自动提交审核

# K3Cloud BOS 设计 Python & C# 单据转换插件 自动提交审核 @[toc](目录) > > ***强调:*** `C#`中使用`this`关键字的,在C#语法中,是可以省略的,但是在 `Python`中,`this`一定要加上!!! # 1.场景 > 二开单据,下推到下游单据 > 1. 下游单据不需要人员操作,自动【提交审核】 # 2.方案 > 二开单据→下推【销售订单】 > 1. 在二开单据中,审核操作下,配置自动下推功能 > 2. 在单据转换插件中,配置插件, > **注意:** > 该方法,一定要注意界面上的下推功能, > 配置好单据转换规则后,如果点击界面下推 ,并且跳转到【销售订单】新增界面时, > 后台会直接生成【销售订单】,如果再点击保存,会出现两张【销售订单】 > 所以,该功能需要进制从下推按钮操作 **配置单据转换插件** ![image.webp](/download/01007213bf0d06774d5a8fd1d7c918099e97.webp) ## 1.依次 保存、提交、审核 ### C# 代码 ```csharp public override void AfterConvert(AfterConvertEventArgs e) { base.AfterConvert(e); string key = e.TargetBusinessInfo.GetEntity(0).Key; List<DynamicObject> list = new List<DynamicObject>(); foreach (var ed in e.Result.FindByEntityKey(key)) { DynamicObject dy = ed.DataEntity; list.Add(dy); } FormMetadata formMetadata = (FormMetadata)AppServiceContext.MetadataService.Load(this.Context, "SAL_SaleOrder", true); IOperationResult operationResult = AppServiceContext.SaveService.Save(this.Context, formMetadata.BusinessInfo, list.ToArray(), null, "");// 保存 if (operationResult.IsSuccess) { List<DynamicObject> list2 = operationResult.SuccessDataEnity.ToList<DynamicObject>(); object[] pkArray = (from p in list2 select p[0]).ToArray();// p[0] 可替代为p["分录Id"] operationResult = AppServiceContext.SubmitService.Submit(this.Context, formMetadata.BusinessInfo, pkArray, "Submit", null);// 提交 if (operationResult.IsSuccess) { List<KeyValuePair<object, object>> pkEntityIds = new List<KeyValuePair<object, object>>(); foreach (var pkValue in pkArray) { pkEntityIds.Add(new KeyValuePair<object, object>(pkValue, "")); } List<object> paras = new List<object>(); paras.Add("1"); paras.Add(""); operationResult = AppServiceContext.SetStatusService.SetBillStatus(this.Context, formMetadata.BusinessInfo, pkEntityIds, paras, "Audit", null);// 审核 } } var s = operationResult.OperateResult.GetFailResult()[0].Message; this.OperationResult.OperateResult = operationResult.OperateResult; this.OperationResult.IsSuccess = operationResult.IsSuccess; this.OperationResult.ValidationErrors = operationResult.ValidationErrors; this.OperationResult.OperateResult = operationResult.OperateResult; } ``` > **审核 代码 替换** > `operationResult = AppServiceContext.SetStatusService.SetBillStatus(this.Context, formMetadata.BusinessInfo, pkEntityIds, paras, "Audit", null);` > 替换为 > `operationResult = AppServiceContext.AuditService.Audit(this.Context, formMetadata.BusinessInfo, pkArray, null);` ```csharp operationResult = AppServiceContext.SetStatusService.SetBillStatus(this.Context, formMetadata.BusinessInfo, pkEntityIds, paras, "Audit", null); // 替换为python 代码中,已经替换为Audit operationResult = AppServiceContext.AuditService.Audit(this.Context, formMetadata.BusinessInfo, pkArray, null); ``` ### 2.python 代码 > **完整代码** ```python def AfterConvert(e): key = e.TargetBusinessInfo.GetEntity(0).Key ls = e.Result.FindByEntityKey(key) ids = List[DynamicObject]() for billObj in ls: ob = billObj.DataEntity ids.Add(ob) pkArray = ids.ToArray() formMetadata = MetaDataServiceHelper.Load(this.Context, "SAL_SaleOrder") operationResult = BusinessDataServiceHelper.Save(this.Context, formMetadata.BusinessInfo, pkArray, None, "") if (operationResult.IsSuccess): ids = operationResult.SuccessDataEnity keys = List[object]() for ob in ids: keys.Add(ob[0]) # 可以使用ob["分录Id"] pkArray = keys.ToArray() operationResult = BusinessDataServiceHelper.Submit(this.Context, formMetadata.BusinessInfo, pkArray,"Submit", None) if operationResult.IsSuccess: operationResult = BusinessDataServiceHelper.Audit(this.Context, formMetadata.BusinessInfo, pkArray, None) this.OperationResult.OperateResult = operationResult.OperateResult this.OperationResult.IsSuccess = operationResult.IsSuccess this.OperationResult.ValidationErrors = operationResult.ValidationErrors this.OperationResult.OperateResult = operationResult.OperateResult ``` > **注意:** > 1. python **`ids = List[DynamicObject] ()`** 注意定义类型,与保存时类型统一 需要引用 `System.Collections.Generic`,使用 > 2. 在`Save` `Sumit` `Audit` 这三个功能中,重载方法特别多,所以,一定要参照C#代码中代码的参数顺序对应好参数类型来写,不然会报 类型错误 > >例:`expected OperateOption got str` 意思是,需要`OperateOption`类型,但是得到了`str` 类型 > > 一般是 `OperateOption option` 和 `string operationNumber`,传参放错位置了 > > ```python > > .Submit(this.Context, formMetadata.BusinessInfo, pkArray, None,"Submit"); # None和"Submit"反了,会提示这个错误 > >.Submit(this.Context, formMetadata.BusinessInfo, pkArray,"Submit", None); # 正确代码 > >``` ## 2.自动提交审核 > 自动提交审核,报错时,不会提示 单据校验详细信息, > 会提示“此操作对该事务的状态无效。” > `IsAutoSubmitAndAudit` 官方不建议使用此方法 ### 1.C#代码 ```csharp public override void AfterConvert(AfterConvertEventArgs e) { string key = e.TargetBusinessInfo.GetEntity(0).Key; List<DynamicObject> list = new List<DynamicObject>(); foreach (var ed in e.Result.FindByEntityKey(key)) { DynamicObject dy = ed.DataEntity; list.Add(dy); } FormMetadata formMetadata = (FormMetadata)AppServiceContext.MetadataService.Load(this.Context, "SAL_SaleOrder", true); var op = OperateOption.Create();// 初始化操作对象 op.SetVariableValue("IsAutoSubmitAndAudit", true);// 设置操作 IOperationResult operationResult = AppServiceContext.SaveService.Save(this.Context, formMetadata.BusinessInfo, list.ToArray(), op, "");// 保存时,自动提交审核 this.OperationResult.OperateResult = operationResult.OperateResult; this.OperationResult.IsSuccess = operationResult.IsSuccess; this.OperationResult.ValidationErrors = operationResult.ValidationErrors; } ``` ### 2.python 代码 ```python def AfterConvert(e): key = e.TargetBusinessInfo.GetEntity(0).Key ls = e.Result.FindByEntityKey(key) ids = List[DynamicObject]() for billObj in ls: ob = billObj.DataEntity ids.Add(ob) pkArray = ids.ToArray() formMetadata = MetaDataServiceHelper.Load(this.Context, "SAL_SaleOrder") option = OperateOption.Create() option.SetVariableValue("IsAutoSubmitAndAudit", True) operationResult = BusinessDataServiceHelper.Save(this.Context, formMetadata.BusinessInfo, pkArray, option, "") if (operationResult.IsSuccess == False): raise Exception("自动提交审核:失败"); ``` ## 返回错误、提示信息 > 直接抛出异常 > **C#** `throw new KDException("", "提示信息!");` > **Python** `raise Exception("提示信息:失败");` > **操作返回结果赋值** 可以显示系统返回的明细信息 > `this.OperationResult.OperateResult = operationResult.OperateResult;` > `this.OperationResult.ValidationErrors = operationResult.ValidationErrors;` > `this.OperationResult.IsSuccess = operationResult.IsSuccess;`

请问可以实现自动下推后,下游单据自动保存、提交并进入审批流吗?


看完了,没二开过,不懂。

#使用技巧# K3Cloud BOS 设计 Python & C# 单据转换插件 自动提交审核

# K3Cloud BOS 设计 Python & C# 单据转换插件 自动提交审核@[toc](目录)> > ***强调:*** `C#`中使用`this`关键字的,在C#语...
点击下载文档
确认删除?
回到顶部
客服QQ
  • 客服QQ点击这里给我发消息