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

NetSuite高级PDF模板无法遍历addCustomDataSource生成的JSON数据

问题:NetSuite高级PDF模板无法遍历自定义数据源的所有行

在NetSuite中使用自定义高级PDF模板,通过N/render模块的addCustomDataSource构建JSON数据源,但PDF仅返回第一行数据,无法打印所有行。已确认数据源配置正常(能获取第一行及指定索引的数据),但循环中用line_no变量无法正确遍历。

编译后的JSON片段

{
   lines: [
      {
         item: "BOM 89",
         custcol_itemdisplayname: "White Cabinet (made to order)",
         unit: "Ea",
         quantity: "1",
         amount: ".00"
      },
      {
         item: "ABC 20019 N",
         custcol_itemdisplayname: "231-670cm Natural",
         unit: "Ea",
         quantity: "1",
         amount: ".00"
      },
      {
         item: "ABC 10040 N",
         custcol_itemdisplayname: "203cm Natural",
         unit: "Ea",
         quantity: "1",
         amount: ".00"
      },
      {
         item: "ABC 10020 N",
         custcol_itemdisplayname: "115cm Natural",
         unit: "Ea",
         quantity: "1",
         amount: ".00"
      }
   ]
}

PDF模板中的循环片段

<#assign line_no = 0>
  <table border="0" cellpadding="1" cellspacing="1" style="width:100%"><!-- start items -->
   <#list results.lines as result>
    <#if result_index==0>
      <tr>
        <td style="height: 29px; width: 120px"><strong>Part Number</strong></td>
        <td style="height: 29px; width: 60px"><strong>&nbsp;</strong></td>
        <td align="left" style="height: 29px; width: 200px; text-align: right"><strong>Description</strong></td>
        <td align="center" style="height: 40px; width: 40px; text-align: center"><strong>Cty of Mfg</strong></td>
        <td align="center" style="height: 29px; text-align: left; width: 60px"><strong>UOM</strong></td>
        <td align="right" style="height: 29px; text-align: right"><strong>Qty</strong></td>
        <td align="right" style="height: 29px; text-align: right"><strong>Value</strong></td>
      </tr>
    </#if>
      <tr>
        <td style="width: 120px">${result[line_no].item}</td>
        <td style="width: 60px"><strong>&nbsp;</strong></td>
        <td align="left" style="width: 200px; text-align: right">${result[line_no].custcol_itemdisplayname}</td>
        <td align="center" style="width: 40px; text-align: center">UK</td>
        <td align="center" style="text-align: right; width: 60px">Pieces</td>
        <td align="right" style="text-align: right">${result[line_no].quantity}</td>
        <td align="right" style="text-align: right">${result[line_no].amount}</td>
      </tr>
      <#assign line_no = line_no + 1> <-----THIS IS USED TO INCREMENT TO NEXT POSITION
  </#list>
</table>

SuiteScript数据源配置片段

renderer.addCustomDataSource({
    format: render.DataSource.OBJECT,
    alias: "results",
    data: searchData
});

问题原因及解决方案

问题原因

错误理解了FreeMarker#list指令的工作逻辑:

  • <#list results.lines as result>中,result已经是lines数组里的单个对象,而非整个数组
  • 用result[line_no].item时,实际是在单个对象上按索引取值,逻辑完全错误。第一次循环line_no=0时碰巧能取到值(部分JS引擎会把对象的索引访问转为属性),但后续line_no递增后,单个对象没有对应索引的属性,自然无法获取数据

解决方案

直接使用result对象本身的属性,无需额外维护line_no变量:

修改后的PDF循环代码:

<#assign line_no = 0>
  <table border="0" cellpadding="1" cellspacing="1" style="width:100%"><!-- start items -->
   <#list results.lines as result>
    <#if result_index==0>
      <tr>
        <td style="height: 29px; width: 120px"><strong>Part Number</strong></td>
        <td style="height: 29px; width: 60px"><strong>&nbsp;</strong></td>
        <td align="left" style="height: 29px; width: 200px; text-align: right"><strong>Description</strong></td>
        <td align="center" style="height: 40px; width: 40px; text-align: center"><strong>Cty of Mfg</strong></td>
        <td align="center" style="height: 29px; text-align: left; width: 60px"><strong>UOM</strong></td>
        <td align="right" style="height: 29px; text-align: right"><strong>Qty</strong></td>
        <td align="right" style="height: 29px; text-align: right"><strong>Value</strong></td>
      </tr>
    </#if>
      <tr>
        <td style="width: 120px">${result.item}</td>
        <td style="width: 60px"><strong>&nbsp;</strong></td>
        <td align="left" style="width: 200px; text-align: right">${result.custcol_itemdisplayname}</td>
        <td align="center" style="width: 40px; text-align: center">UK</td>
        <td align="center" style="text-align: right; width: 60px">${result.unit}</td>
        <td align="right" style="text-align: right">${result.quantity}</td>
        <td align="right" style="text-align: right">${result.amount}</td>
      </tr>
      <!-- 若仍需line_no做其他用途,可保留递增,但无需用它访问result属性 -->
      <#assign line_no = line_no + 1>
  </#list>
</table>

备注:原代码中UOM硬写了"Pieces",建议改为使用数据源里的result.unit,保持数据一致性。


内容的提问来源于stack exchange,提问作者Arif WB

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 12:57:01