How can I access a value in an advanced template data source using addCustomDataSource?

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 this
var 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?
Rookie Asked on January 27, 2021 in Other.
Add Comment
1 Answer(s)
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);
});
 
Intermediate Answered on January 27, 2021.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.
  • This site made possible by our sponsors:   Tipalti   Celigo   Limebox   Become a Sponsor