You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过API填充Business Central的Service Item Worksheet Lines?

问题:Business Central中填充Service Item Worksheet子窗体(Lines)的正确API是什么?

我是Business Central新手,正在找填充Service Item Worksheet子窗体(Lines)的正确API。试过Service Item Lines、Service Lines和Lines(5902)都没用,以下是我当前的尝试代码:

const _createServiceLines = async (serviceOrder, product, serviceItem) => {
  console.log('product', product)

  // Create Service Lines payload for the main product unit
  const payload = {
    body: {
      "Type": "Item",
      "Service_Item_No": serviceItem['Item_No'], // from created order lines
      "No": product.product_id?.itemCode,
      "Document_Type": "Order",
      "Document_No": serviceOrder['No'], // The service order number you created
      "Line_No": 0, // auto-generate
      "Description": product.product_id?.description?.replace(/<[^>]*>/g, '') || '',
      "Quantity": product.quantity || 0,
      "Unit_Price": product.salesPrice || 0,
    }
  }

  console.log('Service Lines Payload:', payload)

  // Create the main Service Lines
  const res = await createServiceLines({ 
    data: payload, 
    config: getBranchConfig.value 
  })

  if (res?.status === 201) {
    const resData = res.data
    console.log('Created Service Lines:', resData)
  } else {
    console.log('Failed to create service lines')
  }
}

回答

你找错了API实体集,Service Item Worksheet的子窗体(Lines)对应的正确OData实体集是**serviceItemWorksheetLines**,之前尝试的几个都对应其他对象:

  • Service Lines是服务订单(Service Order)的行,不是工作表
  • Service Item Lines是服务项目本身关联的行,和工作表无关
  • 直接用Lines(5902)不是标准OData调用方式,BC的OData API用实体集名称而非表号直接访问

修正后的代码示例

调整payload字段,适配Service Item Worksheet Lines的要求,核心是替换Document_Type/Document_No为Worksheet_No(即你要填充的Service Item Worksheet的编号):

const _createServiceItemWorksheetLines = async (worksheetNo, product, serviceItem) => {
  console.log('product', product)

  const payload = {
    body: {
      "Type": "Item",
      "Service_Item_No": serviceItem['Item_No'],
      "No": product.product_id?.itemCode,
      "Worksheet_No": worksheetNo, // 替换为你的Service Item Worksheet编号
      "Description": product.product_id?.description?.replace(/<[^>]*>/g, '') || '',
      "Quantity": product.quantity || 0,
      "Unit_Price": product.salesPrice || 0,
      // Line_No不用手动设置,BC会自动生成
    }
  }

  console.log('Service Item Worksheet Lines Payload:', payload)

  // 调用正确的API实体集,这里假设你的createServiceLines函数可替换为对应serviceItemWorksheetLines的调用
  const res = await createServiceItemWorksheetLines({ 
    data: payload, 
    config: getBranchConfig.value 
  })

  if (res?.status === 201) {
    const resData = res.data
    console.log('Created Service Item Worksheet Lines:', resData)
  } else {
    console.log('Failed to create service item worksheet lines')
  }
}

关键注意点

  1. 确保Worksheet_No对应的Service Item Worksheet已经存在,否则会报错
  2. 字段值要符合BC的验证规则:比如No必须是系统中存在的物料编号,Service_Item_No必须是有效的服务项目编号
  3. 调用API时要使用正确的实体集路径,比如OData端点格式为https://<your-BC-env>/api/v2.0/companies(<companyId>)/serviceItemWorksheetLines

内容的提问来源于stack exchange,提问作者Lekz Flores

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 18:42:35