wasn't sure where to post this, but considering th...
# suitescript
u
wasn't sure where to post this, but considering the issue i'm posting it here. i want to generate a PDF using a suiteliet under Suitescript2.0, with the
N/render
module, the function
render.renderAsPdf()
, binding an Advanced PDF/HTML Template, as well as a Transaction record to it. However, I also want to attach other values from fields within the record, primarily the
mainaddress
field of the Customer field, which can't be accessed through my initial thought of
${record.customer.mainaddress}
within the template. Is there a way to add other values to the
TemplateRenderer
object before it renders the file, that aren't on the record being attached itself? I could go and add custom fields on the record that fetch the values needed, but i feel that that adds unnecessary bloat on a record that doesn't need those details. I plan on just fetching the values i need before calling
renderAsPdf()
. any help would be appreciated, thanks.
c
n
^^ yeah that would be the way to go if you're not pre-loading the data to the record using custom fields.
c
@NElliott You can use the approach CD linked above by fetching the data from the transaction itself and any related records via N/record, N/search, or N/query, composing all required data into a custom object.
n
@Clay Roper Not my question mate ๐Ÿ˜‰
๐Ÿ‘ 1
u
Thanks for the answer, I couldn't really respond in time since I was reassigned for a while on another project. But yes, it seems using
TemplateRenderer.addCustomDataSource()
was the correct approach. For anyone who might search in the future, I was using an Advanced PDF/HTML Template instead of an XML file, so there were minor adjustments to the approach but it is fundamentally the same. I've also learned later on you can, as mentioned by @Clay Roper that you can actually append multiple values in the custom data object, but in my case, I just needed one field. Here are snippets of code: in the suitelet script:
Copy code
//...

let renderer = render.create()
let customdata = { add: customer_record.getValue({ fieldId: "defaultaddress" }) };
renderer.addCustomDataSource({ format: render.DataSource.OBJECT, alias: "custrecord", data: customdata });

//...
in my advanced template:
Copy code
<!-- somewhere in <body> -->

<br/>${custrecord.add}

<!-- ... -->
do note that upon saving, the Advanced Template will call out this declaration as an error, since it doesn't recognize whatever you use for an alias in your
customDataSource
, so don't be afraid if the template complains on saving. There's probably a workaround for negating this popup warning, but since this isn't impeding my workflow and it works as intended, I'm leaving it as is for now.
๐Ÿ‘๐Ÿป 1