跳转至

业务数据管理场景示例

新增数据

同时新增主表和子表数据。

// 获取 Engine 管理对象
H3.IEngine engine = this.Engine;
// 主表表单编码
string masterSchemaCode = "D00021testtable";
// 主表数据 Id
string masterBoId = "bbc60153-19b8-4a57-b074-0c5b77367f8d";
// 子表控件编码
string childSchemaCode = "D00021F7287906e76734bef8819176bc15a8763";
// 获取主表结构体对象
H3.DataModel.BizObjectSchema masterSchema = engine.BizObjectManager.GetSchema(masterSchemaCode);
// 创建主表业务对象
H3.DataModel.BizObject masterBo = new BizObject(engine.Organization, engine.BizObjectManager, masterSchema, H3.Organization.User.SystemUserId);
masterBo.ObjectId = masterBoId;
masterBo.OwnerId = H3.Organization.User.SystemUserId; // 指定拥有者
masterBo["F0000001"] = "test"; // 主表字段赋值
masterBo["F0000003"] = "2025-08-01"; // 主表字段赋值
// 新增子表数据
// 获取子表结构体对象
H3.DataModel.BizObjectSchema childSchema = masterBo.Schema.GetChildSchema(childSchemaCode);
// 定义子表数据集合
List<H3.DataModel.BizObject> newChildBoList = new List<H3.DataModel.BizObject>();
// 创建子表业务对象并添加到首行
H3.DataModel.BizObject childBo1 = new H3.DataModel.BizObject(engine, childSchema, H3.Organization.User.SystemUserId);
childBo1["F0000002"] = "xxxxx";
newChildBoList.Insert(0, childBo1);
// 创建子表业务对象并添加到末行
H3.DataModel.BizObject childBo2 = new H3.DataModel.BizObject(engine, childSchema, H3.Organization.User.SystemUserId);
childBo2["F0000002"] = "yyyyyy";
newChildBoList.Add(childBo2);
// 将子表数据集合赋值到子表控件
masterBo[childSchemaCode] = newChildBoList.ToArray();
// 执行 Create 方法保存主表与子表数据
masterBo.Create();
// 使用返回错误信息的重载方法
// string errorMsg = "";
// masterBo.Create(out errorMsg); 

修改数据

同时修改主表和子表数据。

// 获取 Engine 管理对象
H3.IEngine engine = this.Engine;
// 表单编码
string masterSchemaCode = "D00021testtable";
// 表单数据 Id
string masterBoId = "bbc60153-19b8-4a57-b074-0c5b77367f8d";
// 子表控件编码
string childSchemaCode = "D00021F7287906e76734bef8819176bc15a8763";
// 加载主表业务对象
H3.DataModel.BizObject masterBo = H3.DataModel.BizObject.Load(H3.Organization.User.SystemUserId, engine, masterSchemaCode, masterBoId, false);
// 获取子表结构体对象
H3.DataModel.BizObjectSchema childSchema = masterBo.Schema.GetChildSchema(childSchemaCode);
// 定义新的子表数据集合
List<H3.DataModel.BizObject> newChildBoList = new List<H3.DataModel.BizObject>();
// 获取子表已有数据
H3.DataModel.BizObject[] childBoArray = (H3.DataModel.BizObject[])masterBo[childSchemaCode];
if (childBoArray != null && childBoArray.Length > 0)
{
    foreach (H3.DataModel.BizObject itemBo in childBoArray)
    {
        // 更新已有子表数据
        itemBo["F0000002"] = "更新后的值";
        newChildBoList.Add(itemBo);
    }
}
// 创建子表业务对象并添加到首行
H3.DataModel.BizObject childBo1 = new H3.DataModel.BizObject(engine, childSchema, H3.Organization.User.SystemUserId);
childBo1["F0000002"] = "xxxxx";
newChildBoList.Insert(0, childBo1);
// 创建子表业务对象并添加到末行
H3.DataModel.BizObject childBo2 = new H3.DataModel.BizObject(engine, childSchema, H3.Organization.User.SystemUserId);
childBo2["F0000002"] = "yyyyyy";
newChildBoList.Add(childBo2);
// 将子表数据集合赋值到子表控件
masterBo[childSchemaCode] = newChildBoList.ToArray();
// 执行 Update 方法保存修改
masterBo.Update();

// 使用返回错误信息的重载方法
// string errorMsg = "";
// masterBo.Update(out errorMsg);

删除数据

删除主表数据,同时会删除主表数据对应的子表数据。

// 获取 Engine 管理对象
H3.IEngine engine = this.Engine;
// 表单编码
string masterSchemaCode = "D00021testtable";
// 表单数据 Id
string masterBoId = "bbc60153-19b8-4a57-b074-0c5b77367f8d";
// 子表控件编码
string childSchemaCode = "D00021F7287906e76734bef8819176bc15a8763";
// 加载主表业务对象
H3.DataModel.BizObject masterBo = H3.DataModel.BizObject.Load(H3.Organization.User.SystemUserId, engine, masterSchemaCode, masterBoId, false);
// 执行删除
masterBo.Remove();

通过更新主表数据,删除子表数据。

// 获取 Engine 管理对象
H3.IEngine engine = this.Engine;
// 表单编码
string masterSchemaCode = "D00021testtable";
// 表单数据 Id
string masterBoId = "bbc60153-19b8-4a57-b074-0c5b77367f8d";
// 子表控件编码
string childSchemaCode = "D00021F7287906e76734bef8819176bc15a8763";
// 加载主表业务对象
H3.DataModel.BizObject masterBo = H3.DataModel.BizObject.Load(H3.Organization.User.SystemUserId, engine, masterSchemaCode, masterBoId, false);
// 获取子表结构体对象
H3.DataModel.BizObjectSchema childSchema = masterBo.Schema.GetChildSchema(childSchemaCode);
// 定义新的子表数据集合
List<H3.DataModel.BizObject> newChildBoList = new List<H3.DataModel.BizObject>();
// 获取子表已有数据
H3.DataModel.BizObject[] childBoArray = (H3.DataModel.BizObject[])masterBo[childSchemaCode];
if (childBoArray != null && childBoArray.Length > 0)
{
    foreach (H3.DataModel.BizObject itemBo in childBoArray)
    {
        // 根据条件过滤需删除的子表记录
        if (itemBo["F000002"] == "需要删除")
        {
            continue;
        }
        // 保留的子表记录添加到新集合
        newChildBoList.Add(itemBo);
    }
}
// 将子表数据集合赋值到子表控件
masterBo[childSchemaCode] = newChildBoList.ToArray();
// 执行 Update 方法保存修改
masterBo.Update();

批量操作数据

使用 BulkCommit 可提升大批量数据操作的执行效率。

BizObject 对象的 UpdateRemove 方法都可以参考该用法。

H3.DataModel.BulkCommit commit = new H3.DataModel.BulkCommit();
H3.DataModel.BizObjectSchema schema = this.Request.Engine.BizObjectManager.GetPublishedSchema("D0001001");
for(int i = 0;i < 1000; i++) {
       H3.DataModel.BizObject obj = new   H3.DataModel.BizObject(this.Request.Engine,schema,this.Request.UserContext.UserId   );
       obj["F000001"] = 1;
       obj.Create(commit); // 已存在对象可使用 obj.Update(commit), obj.Remove(commit)

}
string errorMsg = null;
commit.Commit(this.Request.Engine.BizObjectManager, out errorMsg); // 批量提交减少服务交互

一键清除表单的所有数据

该操作不会检查数据的引用关系,请谨慎操作。

string schemaCode = "D00002Lead";
// 清除表单 D00002Lead 的所有数据
this.Request.Engine.BizObjectManager.Clear(schemaCode); 

⚠️ 注意
数据清除后不可恢复。

通过数据 ID 查询单条业务数据

需更新业务数据时使用 BizObject 方式,BizObject 不能序列化所以不可直接返回到前端。
仅读取数据时使用 H3.Data.Serialization.VirtualObject 方式,可序列化可直接返回到前端。

// 获取 Engine 管理对象
H3.IEngine engine = this.Engine;
// 表单编码
string masterSchemaCode = "D00021testtable";
// 表单数据 Id
string masterBoId = "bbc60153-19b8-4a57-b074-0c5b77367f8d";
// 子表控件编码
string childSchemaCode = "D00021F7287906e76734bef8819176bc15a8763";
// 加载主表业务对象
H3.DataModel.BizObject masterBo = H3.DataModel.BizObject.Load(H3.Organization.User.SystemUserId, engine, masterSchemaCode, masterBoId, false);
// 获取 Engine 管理对象
H3.IEngine engine = this.Engine;
// 表单编码
string masterSchemaCode = "D00021testtable";
// 表单数据 Id
string masterBoId = "bbc60153-19b8-4a57-b074-0c5b77367f8d";
// 子表控件编码
string childSchemaCode = "D00021F7287906e76734bef8819176bc15a8763";

Dictionary<string, List<BizObjectFileHeader>> fileTable = null;
Dictionary<string, BizObjectHeader> relatedHeaders = null;
Dictionary<string, string> unitTable = null;

// 获取虚对象数据
H3.Data.Serialization.VirtualObject vo = engine.BizObjectManager.LoadBizObject(
    userId,
    masterSchemaCode,
    masterBoId,
    requireRelatedObjects,
    returnColumns,
    out this._fileTable,
    out this._associatedBizObjectHeaders,
    out this._unitNameTable);

通过条件查询业务数据

需更新业务数据时使用 BizObject 方式,BizObject 不能序列化所以不可直接返回到前端。
仅读取数据时使用 H3.Data.Serialization.VirtualObject 方式,可序列化可直接返回到前端。

// 构造 Filter
// selectedObjectIds 为选中行数据 Id 集合
private H3.Data.Filter.Filter BuildFilter(string[] selectedObjectIds, int toRowNum)
{
    H3.Data.Filter.Filter filter = new H3.Data.Filter.Filter();
    And and = new And();
    if (selectedObjectIds != null && selectedObjectIds.Length > 0)
    {
        and.Add(new ItemMatcher("ObjectId", H3.Data.ComparisonOperatorType.In, selectedObjectIds));
    }
    and.Add(new ItemMatcher("GeneratedProcessBom", H3.Data.ComparisonOperatorType.NotEqual, "是"));
    and.Add(new ItemMatcher("Status", H3.Data.ComparisonOperatorType.Equal, (int)BizObjectStatus.Effective));
    filter.Matcher = and;
    filter.FromRowNum = 0;
    filter.ToRowNum = toRowNum; // 查询条数
    filter.SortByCollection = new SortBy[]
    {
        new SortBy(BizObjectSchema.PropertyName_CreatedTime, SortDirection.Ascending)
    };
    return filter;
}

// 执行查询
H3.Data.Filter.Filter filter = BuildFilter(new string[] {"id1","id2"}, 10);
H3.DataModel.BizObjectSchema schema = this.Engine.BizObjectManager.GetPublishedSchema("表单编码");
if(schema == null)
{
    throw new H3Exception("表单已被删除");
}
BizObject[] bizobjectArray = BizObject.GetList(this.Engine,  // 引擎对象
                                               H3.Organization.User.SystemUserId, // 用户身份
                                               schema, // 表单业务模式
                                               H3.DataModel.GetListScopeType.Own, 
                                               filter);
// 构造 Filter
// selectedObjectIds 为选中行数据 Id 集合
private Filter BuildFilter(string[] selectedObjectIds, int toRowNum)
{
    H3.Data.Filter.Filter filter = new H3.Data.Filter.Filter();
    And and = new And();
    if (selectedObjectIds != null && selectedObjectIds.Length > 0)
    {
        and.Add(new ItemMatcher("ObjectId", H3.Data.ComparisonOperatorType.In, selectedObjectIds));
    }
    and.Add(new ItemMatcher("GeneratedProcessBom", H3.Data.ComparisonOperatorType.NotEqual, "是"));
    and.Add(new ItemMatcher("Status", H3.Data.ComparisonOperatorType.Equal, (int)BizObjectStatus.Effective));
    filter.Matcher = and;
    filter.FromRowNum = 0;
    filter.ToRowNum = toRowNum;
    filter.SortByCollection = new SortBy[]
    {
        new SortBy(BizObjectSchema.PropertyName_CreatedTime, SortDirection.Ascending)
    };
    return filter;
}

public static H3.Data.Serialization.VirtualObject[] GetVirtualObjects(IBizObjectManager bizObjectManager, 
                                                H3.Data.Filter.Filter filter, 
                                                string schemaCode)
{
    int count = 0;
    Dictionary<string, BizObjectHeader> relatedHeaders = null;
    Dictionary<string, string> unitTable = null;
    H3.Data.Serialization.VirtualObject[] vos = bizObjectManager.GetList(H3.Organization.User.SystemUserId,
        schemaCode,
        H3.DataModel.GetListScopeType.GlobalAll,
        filter,
        out count,
        false,
        out relatedHeaders,
        out unitTable);
    return vos;
}

// 执行查询
H3.Data.Filter.Filter filter = BuildFilter(new string[] {"id1","id2"}, 10);
H3.Data.Serialization.VirtualObject[] vos = GetVirtualObjects(this.Engine.BizObjectManager, filter, "表单编码");

根据表单编码判断当前表单是否开启流程

public bool IsWorkflowNode(string schemaCode)
{
    H3.App.FunctionNode node = this.Engine.AppPackageManager.GetFunctionNode(schemaCode);
    if(node == null)
    {
        throw new H3Exception("表单已被删除");
    }
    if(node.NodeType == H3.App.FunctionNodeType.WorkflowModule)
    {
        return true;
    }    
    return false;
}

自定义代码触发自动化

// 自动化编码(从自动化设计页面获取)
const string AutomationCode_StartTask = "A20250311180138"; 
string[] bizObjectIds = new string[1];
// 触发的业务数据 Id
bizObjectIds[0] = this.Request.BizObjectId; 
// 触发的表单编码
string schemaCode = this.Request.SchemaCode; 
this.Engine.AutomationManager.ExecuteAutomation(schemaCode, 
                                                AutomationCode_StartTask, 
                                                bizObjectIds, 
                                                null);