关于执行计划的事务化问题
金蝶云社区-jsnsoft_1
jsnsoft_1
0人赞赏了该文章 1,464次浏览 未经作者许可,禁止转载编辑于2015年08月09日 18:41:07

实现功能:在后台事务中,实现客户档案的定时导入;
当客户数据有问题时,回滚整个导入数据。

1.我们建立了一个后台事务public class ScheduleServiceTest : Kingdee.BOS.Contracts.IScheduleService
run方法如下:
public void Run(Context ctx, Schedule schedule)
{
transactionTest t=new transactionTest(ctx);
t.EndOperationTransaction();
}

2.现在的问题是,在EndOperationTransaction方法中,并没有实现事务化,客户000001依然可以成功导入,请高手指导一下,如何事务化。
代码如下:

namespace CTC.DataSyncTask
{
public class transactionTest : AbstractOperationServicePlugIn
{
Context ctx = null;
public transactionTest(Context newctx)
{
ctx = newctx;
}
public void EndOperationTransaction()
{
IMetaDataService metaService = Kingdee.BOS.App.ServiceHelper.GetService();
//获取客户基本资料元数据
FormMetadata matMetaData = metaService.Load(ctx, "BD_Customer") as FormMetadata;
//业务元数据BusinessInfo
BusinessInfo customer_info = matMetaData.BusinessInfo;
//创建客户基本资料空数据包
DynamicObject toSaveCustomer = new DynamicObject(matMetaData.BusinessInfo.GetDynamicObjectType());
//客户名称*|客户编码*|客户简称|客户SOAID*|通讯地址|联系电话
/**
* TODO:给各字段属性赋值,比如给FName字段赋值
* */
//普通文本、数值、日期等字段赋值
DynamicObject[] DynamicObjectList = new DynamicObject[2];
Field fCommonField = customer_info.GetField("FName");
fCommonField.DynamicProperty.SetValue(toSaveCustomer, "测试客户");
fCommonField = customer_info.GetField("FNumber");
fCommonField.DynamicProperty.SetValue(toSaveCustomer, "000001");
fCommonField = customer_info.GetField("FShortName");
fCommonField.DynamicProperty.SetValue(toSaveCustomer, "测客");
fCommonField = customer_info.GetField("FADDRESS");
fCommonField.DynamicProperty.SetValue(toSaveCustomer, "银海大厦");
fCommonField = customer_info.GetField("FTEL");
fCommonField.DynamicProperty.SetValue(toSaveCustomer, "110");
fCommonField = customer_info.GetField("FPriority");
fCommonField.DynamicProperty.SetValue(toSaveCustomer, 1);
//给创建组织(基础资料)字段赋值
BaseDataField FOrg = customer_info.GetField("FCreateOrgId") as BaseDataField;
//先给内码赋值
int org_id = 1;
FOrg.RefIDDynamicProperty.SetValue(toSaveCustomer, org_id);
//按内码获取基础数据包
var org_obj = Kingdee.BOS.ServiceHelper.BusinessDataServiceHelper.LoadSingle(ctx, org_id, FOrg.RefFormDynamicObjectType);
//给数据包赋值
FOrg.DynamicProperty.SetValue(toSaveCustomer, org_obj);

//给使用组织组织(基础资料)字段赋值
FOrg = customer_info.GetField("FUseOrgId") as BaseDataField;
FOrg.RefIDDynamicProperty.SetValue(toSaveCustomer, org_id);
org_obj = Kingdee.BOS.ServiceHelper.BusinessDataServiceHelper.LoadSingle(ctx, org_id, FOrg.RefFormDynamicObjectType);
FOrg.DynamicProperty.SetValue(toSaveCustomer, org_obj);

//给结算币别(基础资料)字段赋值
BaseDataField FCURRENCY = customer_info.GetField("FTRADINGCURRID") as BaseDataField;
//先给内码赋值
int currency_id = 1;
FCURRENCY.RefIDDynamicProperty.SetValue(toSaveCustomer, currency_id);
//按内码获取基础数据包
var currency_obj = Kingdee.BOS.ServiceHelper.BusinessDataServiceHelper.LoadSingle(ctx, currency_id, FCURRENCY.RefFormDynamicObjectType);
//给数据包赋值
FCURRENCY.DynamicProperty.SetValue(toSaveCustomer, currency_obj);

//给国家(基础资料)字段赋值
BaseDataField country = customer_info.GetField("FCOUNTRY") as BaseDataField;
//先给内码赋值
string country_id = "46a524cf-5797-4e46-bd0a-7203fc426d9c";
country.RefIDDynamicProperty.SetValue(toSaveCustomer, country_id);
//按内码获取基础数据包
var country_obj = Kingdee.BOS.ServiceHelper.BusinessDataServiceHelper.LoadSingle(ctx, country_id, country.RefFormDynamicObjectType);
//给数据包赋值
country.DynamicProperty.SetValue(toSaveCustomer, country_obj);
//禁用状态
Field forbidstatus = customer_info.GetField("FForbidStatus");
forbidstatus.DynamicProperty.SetValue(toSaveCustomer, "A");
//是否交易客户
Field fistrade = customer_info.GetField("FISTRADE");
fistrade.DynamicProperty.SetValue(toSaveCustomer, true);
DynamicObjectList[0] = toSaveCustomer;
DynamicObject toSaveCustomerClone = new DynamicObject(matMetaData.BusinessInfo.GetDynamicObjectType());
toSaveCustomerClone = toSaveCustomer.Clone(true, true) as DynamicObject;
fCommonField = customer_info.GetField("FName");
fCommonField.DynamicProperty.SetValue(toSaveCustomerClone, "测试客户11111123");
fCommonField = customer_info.GetField("FNumber");
//故意让此段报错,超长编码
fCommonField.DynamicProperty.SetValue(toSaveCustomerClone, "000001111111111111111111111111111111111112222222222222222222222222211111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111231");
DynamicObjectList[1] = toSaveCustomerClone;

//然后调用保存服务接口,得到保存结果result
IOperationResult result = Kingdee.BOS.ServiceHelper.BusinessDataServiceHelper.Save(ctx, customer_info, DynamicObjectList, null, "Save");
System.Collections.Generic.IEnumerable iEnumerable = result.SuccessDataEnity;

}
}
}