How can I access a value in an advanced template data source using addCustomDataSource?
Answered
I’m trying to pass some information into a Custom template using the following,
var items = getBulkItems(parameters);
renderer.addCustomDataSource({
alias: "ITEMS",
format: render.DataSource.JSON,
data: JSON.stringify(items)
});
The
items I am passing in looks like thisvar items = { 'items': [], 'units': units }; // Array items is populated later
I am able to get and render the Array of Items in the template using,
<table style="width: 100%; margin-top: 5px;" colspan="10">
<#if ITEMS?has_content>
<#list ITEMS.items as item>
<#if item_index==0>
<thead>
<tr>
<th colspan="2">Item - Description</th>
</tr>
</thead>
</#if>
<tr>
<td colspan="2">${item.name}</td>
</tr>
</#list>
</#if>
</table>
But I am unable to render the units passed in.
<#if ITEMS?has_content>
<#if ITEMS.units?has_content> Units: ${ITEMS.units} </#if>
</#if>
Sorry this was rather long, but why can I not access the units variable?
Best answer
Your template and code looks right and works for me. Make sure that your units variable has a valid value in it.
The code I tried that works looks like
require(["N/render"], function (render) {
var renderer = render.create();
var data = {
items: [{ name: "name0" }, { name: "name1" }],
units: "my unit value",
};
renderer.templateContent =
"<#if ITEMS?has_content><#if ITEMS.units?has_content> Units: ${ITEMS.units} </#if></#if>";
renderer.addCustomDataSource({
format: render.DataSource.JSON,
alias: "ITEMS",
data: JSON.stringify(data),
});
var output = renderer.renderAsString();
log.debug("output", output);
});



