这是一个非常有趣的观察,当使用如下代码试图生成一个PDF时,SuiteScript会报错:

    let renderer = render.create();
    // Assembly build PDF label template
    let xmlTemplateFile = file.load({ id: 123123123 });
    renderer.templateContent = xmlTemplateFile.getContents();
    renderer.addCustomDataSource({ format: 'OBJECT', alias: 'records', data: {record:jsonArr} });
    let buildPDF = renderer.renderAsPdf();
    buildPDF.name = 'output.pdf';
    context.response.writeFile({ file: buildPDF });

这里如果format是OBJECT,会报错提示

You have supplied an invalid value for data format: JSON: The content of elements must consist of well-formed character data or markup.

如果把format设置为JSON,则报错提示为

You have supplied an invalid value for data format: JSON: The content of elements must consist of well-formed character data or markup

我尝试过使用JSON.parse()或者是用regex把输入的jsonArr进行整理,但是没有任何效果。最后我发现问题出现在json存在一个键是纯数字的,整理之后问题消失

//json with numeric keys
{
   lineItem: {
       "1": {"name": "pencil"},
       "2": {"name": "rubber"}
   }
}

//json with numeric keys removed
{
   lineItem: {
       "line1": {"name": "pencil"},
       "line2": {"name": "rubber"}
   }
}

作为JSON,它的键可以是数字或者字符串,但是可能是NetSuite内部的原因,键为纯数字的时候会报错,建议开发的时候注意。

Last modified: 05/03/2024

Author

Comments

Write a Reply or Comment

Your email address will not be published.