如何通过自定义API在Business Central中实现深度插入?
Business Central自定义API实现深度插入(表头+明细单次提交)
尝试通过单次调用Business Central自定义API完成表头+多行明细的插入操作,当前已创建父API页面和子页面,但提交JSON后仅表头数据生成,明细行未被插入。
当前代码与请求情况
父API页面(60000)
page 60000 "Sales Order Inserts MIM API" { PageType = API; SourceTable = "Sales Order Inserts MIM"; Caption = 'Sales Order Inserts MIM API'; EntitySetName = 'SalesOrderInserts'; EntityName = 'SalesOrderInsert'; APIPublisher = 'Kuhicop'; APIGroup = 'Kuhicop'; DelayedInsert = true; APIVersion = 'v1.0'; layout { area(content) { repeater(Group) { field("BatchID"; Rec."Batch ID") { ApplicationArea = All; } field("BatchDate"; Rec."Batch Date") { ApplicationArea = All; } part(SalesOrderLines; 60001) { ApplicationArea = All; SubPageLink = "Batch ID" = field("Batch ID"); } } } } }
子页面(60001)
page 60001 "Sales Orders MIM" { ApplicationArea = All; Caption = 'Sales Orders MIM'; SourceTable = "Sales Orders MIM"; APIGroup = 'Kuhicop'; APIPublisher = 'Kuhicop'; APIVersion = 'v1.0'; DelayedInsert = true; EntityName = 'SalesOrder'; EntitySetName = 'SalesOrders'; PageType = API; layout { area(content) { repeater(General) { field("BatchID"; Rec."Batch ID") { ToolTip = 'Batch ID'; } field("DocumentNo"; Rec."Document No.") { ToolTip = 'Specifies the value of the Document No. field.'; } field("LineNo"; Rec."Line No.") { ToolTip = 'Specifies the value of the Line No. field.'; } } } } }
请求JSON
{ "BatchID": "BATCH003", "BatchDate": "2024-05-09", "salesOrders": [ { "DocumentNo": "PED002", "LineNo": 10000, "BatchID": "BATCH003" } ] }
响应结果
{ "@odata.context": "https://api.businesscentral.dynamics.com/v2.0/{{TENANT_ID}}/{{COMPANY}}/api/Kuhicop/Kuhicop/v1.0/$metadata#companies(c6452bcb-c1b3-eb11-9b52-002248818d7e)/SalesOrderInserts/$entity", "@odata.etag": "W/\"JzE5OzgwMDc0MzY2NzQ0NjcxOTQxOTExOzAwOyc=\"", "BatchID": "BATCH003", "BatchDate": "2024-05-09", "salesOrders": [] }
解决步骤
要实现深度插入,需对页面代码做以下关键修改:
- 启用父API页面的DeepInsert属性:在父API页面中添加
DeepInsert = true;,这是允许嵌套实体插入的核心配置。 - 修改子页面的PageType为ListPart:子页面不能是独立的API页面,需改为ListPart类型作为父页面的内嵌部件,同时移除所有API相关属性(APIPublisher、APIGroup等)。
- 对齐JSON子节点名称与部件名称:通过在父页面的part定义中添加
Name = 'salesOrders';,指定JSON中的子节点字段名,确保和请求中的key对应。
修改后的代码
父API页面(60000)
page 60000 "Sales Order Inserts MIM API" { PageType = API; SourceTable = "Sales Order Inserts MIM"; Caption = 'Sales Order Inserts MIM API'; EntitySetName = 'SalesOrderInserts'; EntityName = 'SalesOrderInsert'; APIPublisher = 'Kuhicop'; APIGroup = 'Kuhicop'; DelayedInsert = true; APIVersion = 'v1.0'; DeepInsert = true; // 新增:启用深度插入 layout { area(content) { repeater(Group) { field("BatchID"; Rec."Batch ID") { ApplicationArea = All; } field("BatchDate"; Rec."Batch Date") { ApplicationArea = All; } part(SalesOrderLines; 60001) { ApplicationArea = All; SubPageLink = "Batch ID" = field("Batch ID"); Name = 'salesOrders'; // 指定JSON中的字段名,匹配请求key } } } } }
子页面(60001)
page 60001 "Sales Orders MIM" { ApplicationArea = All; Caption = 'Sales Orders MIM'; SourceTable = "Sales Orders MIM"; DelayedInsert = true; PageType = ListPart; // 修改为ListPart类型 layout { area(content) { repeater(General) { field("BatchID"; Rec."Batch ID") { ToolTip = 'Batch ID'; } field("DocumentNo"; Rec."Document No.") { ToolTip = 'Specifies the value of the Document No. field.'; } field("LineNo"; Rec."Line No.") { ToolTip = 'Specifies the value of the Line No. field.'; } } } } }
验证请求
使用原JSON请求即可提交,修改后表头和明细行将同时被插入,响应中会返回包含明细数据的完整实体。
内容的提问来源于stack exchange,提问作者kuhi
相关产品推荐
相关产品推荐

