如何扩展Business Central /customers端点返回客户默认维度
解决Business Central /customers API端点返回完整维度列表的方案
你当前的代码存在两个关键问题:
TDimensions.Get()未指定过滤条件,会直接抛出记录不存在的错误(默认维度表包含多条关联记录)- 单个
Text变量只能存储单个维度值,无法返回完整的维度列表
以下是可行的实现方案:
1. 核心逻辑修正:关联客户查询维度
要获取当前客户的所有维度,需在Default Dimension表中按客户表ID和客户编号过滤,再遍历结果构造结构化数据。
2. 完整扩展代码示例
pageextension 50100 CustomerAPIExt extends "Customer API" { layout { addafter("Payment Terms Code") { field("CustomerDimensions"; CustomerDimensionsJson) { Caption = '客户维度'; ApplicationArea = All; } } } var CustomerDimensionsJson: JsonArray; trigger OnAfterGetRecord() var DefaultDimension: Record "Default Dimension"; DimensionValue: Record "Dimension Value"; DimJsonObj: JsonObject; begin CustomerDimensionsJson.Clear(); // 每次获取客户记录时清空数组 // 过滤当前客户关联的所有默认维度 DefaultDimension.Reset(); DefaultDimension.SetRange("Table ID", Database::Customer); DefaultDimension.SetRange("No.", Rec."No."); if DefaultDimension.FindSet() then begin repeat // 关联维度值表获取维度名称(可选,不需要可跳过) DimensionValue.Get(DefaultDimension."Dimension Code", DefaultDimension."Dimension Value Code"); // 构造单个维度的JSON对象 DimJsonObj.Clear(); DimJsonObj.Add('DimensionCode', DefaultDimension."Dimension Code"); DimJsonObj.Add('DimensionValueCode', DefaultDimension."Dimension Value Code"); DimJsonObj.Add('DimensionName', DimensionValue.Name); // 将单个维度对象加入数组 CustomerDimensionsJson.Add(DimJsonObj); until DefaultDimension.Next() = 0; end; end; }
3. API返回效果
发布扩展后调用/customers端点,每个客户对象会新增CustomerDimensions字段,返回格式示例:
"CustomerDimensions": [ { "DimensionCode": "DEPT", "DimensionValueCode": "SALES", "DimensionName": "销售部门" }, { "DimensionCode": "PROJ", "DimensionValueCode": "PROJ01", "DimensionName": "Alpha项目" } ]
若不需要维度名称,可删除DimensionValue表的关联逻辑,直接使用Default Dimension表的字段构造JSON。
内容的提问来源于stack exchange,提问作者Jack Bracey
相关产品推荐
相关产品推荐

