如何在Adv PDF模板中访问addRecord添加的Sales Order数据?
NetSuite高级PDF模板:获取Sales Order折扣行项目方案
问题背景
当前处于Item Fulfillment记录场景,需要获取关联Sales Order的数据。已通过以下代码在渲染器中加载了Item Fulfillment和对应的Sales Order记录:
renderer.addRecord('record', record.load({ type: record.Type.ITEM_FULFILLMENT, id: 1025639 })); renderer.addRecord('record', record.load({ type: record.Type.SALES_ORDER, id: 1022380 }));
目前PDF模板能正常显示Item Fulfillment的详情,但无法获取并展示Sales Order的行项目,需要实现显示SO中的折扣行项目。现有模板代码如下:
<#if record.item?has_content> <p style="font-size: 12px; padding-bottom: 0;margin-bottom:0"> These items will arrive soon! </p> <table class="itemtable" style="width: 100%; margin-top: 10px;"> <thead> <tr> <th colspan="12">Item</th> <th align="right" colspan="3">Shipped</th> <th align="right" colspan="3">Price</th> <th align="right" colspan="3">Excise</th> <th align="right" colspan="3">Amount</th> </tr> </thead> <#list record.item?sort_by("displayname") as tranline><tr> <td colspan="12"><span class="itemname">${tranline.description}</span></td> <#setting number_format="0.##"> <td align="right" colspan="3">${tranline.quantity}</td> <#setting number_format="currency"> <td align="right" colspan="3">${tranline.custcol_nscs_itemsalesrate}</td> <td align="right" colspan="3">${tranline.custcol_excisecalculation}</td> <td align="right" colspan="3"><#assign lineitemtotal=(tranline.custcol_nscs_itemsalesrate * tranline.quantity) + (tranline.custcol_excisecalculation * tranline.quantity)>${lineitemtotal}</td> </tr> </#list></table> </#if>
解决方案
1. 调整渲染器代码:为Sales Order设置独立记录别名
当前代码中两次调用addRecord('record', ...)会覆盖第一个记录,导致模板只能访问最后加载的记录。需为Sales Order指定单独别名,比如salesorder:
// 加载Item Fulfillment记录,保留原别名 renderer.addRecord('record', record.load({ type: record.Type.ITEM_FULFILLMENT, id: 1025639 })); // 加载Sales Order记录,使用新别名 renderer.addRecord('salesorder', record.load({ type: record.Type.SALES_ORDER, id: 1022380 }));
2. 修改PDF模板代码:单独渲染Sales Order折扣行
在模板中新增区块,专门遍历Sales Order行项目,筛选折扣类型行并展示:
<!-- 保留原有Item Fulfillment商品行展示代码,已翻译为中文 --> <#if record.item?has_content> <p style="font-size: 12px; padding-bottom: 0;margin-bottom:0"> 以下商品即将送达! </p> <table class="itemtable" style="width: 100%; margin-top: 10px;"> <thead> <tr> <th colspan="12">商品</th> <th align="right" colspan="3">已发货数量</th> <th align="right" colspan="3">单价</th> <th align="right" colspan="3">消费税</th> <th align="right" colspan="3">金额</th> </tr> </thead> <#list record.item?sort_by("displayname") as tranline><tr> <td colspan="12"><span class="itemname">${tranline.description}</span></td> <#setting number_format="0.##"> <td align="right" colspan="3">${tranline.quantity}</td> <#setting number_format="currency"> <td align="right" colspan="3">${tranline.custcol_nscs_itemsalesrate}</td> <td align="right" colspan="3">${tranline.custcol_excisecalculation}</td> <td align="right" colspan="3"><#assign lineitemtotal=(tranline.custcol_nscs_itemsalesrate * tranline.quantity) + (tranline.custcol_excisecalculation * tranline.quantity)>${lineitemtotal}</td> </tr> </#list></table> </#if> <!-- 新增:Sales Order折扣行展示区块 --> <#if salesorder.item?has_content> <p style="font-size: 12px; padding-bottom: 0;margin-bottom:0; margin-top:20px"> 订单折扣明细: </p> <table class="itemtable" style="width: 100%; margin-top: 10px;"> <thead> <tr> <th colspan="15">折扣项目</th> <th align="right" colspan="6">折扣金额</th> </tr> </thead> <#list salesorder.item as soLine> <!-- 筛选折扣类型行,NetSuite默认折扣行itemtype为"Discount",可根据实际字段调整 --> <#if soLine.itemtype == "Discount"> <tr> <td colspan="15"><span class="itemname">${soLine.description}</span></td> <#setting number_format="currency"> <td align="right" colspan="6">${soLine.amount}</td> </tr> </#if> </#list> </table> </#if>
关键说明
- 记录别名:必须为不同记录设置不同别名,避免覆盖,模板中通过别名(如
salesorder)访问对应记录字段。 - 折扣行筛选:若使用自定义折扣标识字段,需替换
soLine.itemtype == "Discount"为实际判断条件(如soLine.custcol_is_discount == true)。 - 字段适配:可根据业务需求调整折扣行显示字段,比如折扣率、关联商品等。
内容的提问来源于stack exchange,提问作者Mango
相关产品推荐
相关产品推荐

