Python服务插件为多选基础资料赋值
解决思路:
通过C#实现该功能,方便调试,参考C#代码编写Python脚本。
主题:
单据保存时,使用Python脚本通过服务插件自动为多选基础资料字段赋值。
Python脚本:
assignColl = relRow["Assign"] #relRow为单据头或分录行对象,Assign为多选基础资料字段实体属性名,示例中该字段值为用户
if (len(assignColl) == 0):
#多选基础资料赋值
userId = this.Context.UserId
userObj = LoadBDFieldObject('SEC_User', userId) #构建基础资料数据对象
#itemObj = DynamicObject(assignColl.DynamicCollectionItemPropertyType) #创建子项方式一
itemObj = assignColl.DynamicCollectionItemPropertyType.CreateInstance() #创建子项方式二
itemObj["Assign_Id"] = userId
itemObj["Assign"] = userObj
assignColl.Add(itemObj) #将子项添加到多选基础资料字段对象
#获取基础资料
def LoadBDFieldObject(formId, id):
meta = MetaDataServiceHelper.Load(this.Context, formId)
queryParam = QueryBuilderParemeter()
queryParam.FormId = formId
queryParam.BusinessInfo = meta.BusinessInfo
queryParam.FilterClauseWihtKey = "{0}='{1}'".format(meta.BusinessInfo.GetForm().PkFieldName,id)
bdObjs = BusinessDataServiceHelper.Load(this.Context, meta.BusinessInfo.GetDynamicObjectType(), queryParam)
if (bdObjs is None or len(bdObjs) == 0):
return None;
return bdObjs[0]
C#脚本:
DynamicObjectCollection assignColl = relRow["Assign"] as DynamicObjectCollection;
if (assignColl.Count() == 0)
{
long userId = this.Context.UserId;
DynamicObject userObj = LoadBDFieldObject("SEC_User", userId);
DynamicObject itemObj = new DynamicObject(assignColl.DynamicCollectionItemPropertyType);
//DynamicObject itemObj = assignColl.DynamicCollectionItemPropertyType.CreateInstance() as DynamicObject;
itemObj["Assign_Id"] = userId;
itemObj["Assign"] = userObj;
assignColl.Add(itemObj);
}
// 获取基础资料
public DynamicObject LoadBDFieldObject(string formId, object id) {
FormMetadata meta = (FormMetadata)MetaDataServiceHelper.Load(this.Context, formId);
QueryBuilderParemeter queryParam = new QueryBuilderParemeter();
queryParam.FormId = formId;
queryParam.BusinessInfo = meta.BusinessInfo;
queryParam.FilterClauseWihtKey = string.Format( "{0}='{1}'", meta.BusinessInfo.GetForm().PkFieldName, id);
DynamicObject[] bdObjs = BusinessDataServiceHelper.Load(this.Context, meta.BusinessInfo.GetDynamicObjectType(), queryParam);
if (bdObjs ==null || bdObjs.Count() == 0)
return null;
return bdObjs[0];
}