Hi there, Is it possible to add additional data t...
# advancedpdf
c
Hi there, Is it possible to add additional data to a transaction's default PDF/HTML template? Basically my sales order form points to an advanced PDF/HTML template. However I need to pull in some additional data from a one to many related record. I'm aware of addCustomDataSource, however I'm not sure how or when to attach that to the existing advanced pdf/html template. My initial thought was to do it in the user event, beforeLoad where scriptcontext.type == 'Print'. Is it possible to attach JSON data to the record in this beforeLoad context? Or do you need a custom suitelet and custom print button to accomplish this?
h
recently answered almost the same question in #C29HQS63G you could create a UE script using PRINT context type and deploy it to Invoice. Here's a snippet of UE:
Copy code
const beforeLoad = (scriptContext) => {
    try {
        if (scriptContext.type !== scriptContext.UserEventType.PRINT) return;
        const {newRecord, form} = scriptContext;
        const  linesField = form.addField({
            id: "custpage_custom_lines",
            label: 'Custom Lines',
            type: ui.FieldType.INLINEHTML,
        })

        const searchResults = // create a function to get search results
        linesField.defaultValue = JSON.stringify(searchResults)


    } catch (error) {
        log.error('ERROR in beforeLoad', error.toString());
    }
}
You could get the custom lines in the pdf as follows:
<#assign data = record.custpage_custom_lines?eval>
<#list data as line>//doSomething</#list>
You might only be able to use this in the <body> of the pdf template. If you need to use this in <head> then I think you could inject a Suitelet in the <head> then use it
if you're rendering the transaction PDF in a script then you could use addCustomDataSource to add custom JSON objects when rendering the pdf as well
c
Awesome, I think the piece I was missing was the #assign to parse the JSON in the template. Will give that a try!
s
@Cory Weiner it should be noted, that this method will only work on the PRINT action... If you are emailing a PDF from the transaction record, this will not trigger the Print context script, even though the email itself generates the PDF it still won't trigger print context script. Depending on the size of the dataset, and whether it's to be dynamic or not, you may need a UE/afterSubmit script on the transaction to store the added data as a JSON/Long Text field on the record.
🙌 1
h
@Stefan Reeder very interesting point. Would it work if we included EMAIL execution type for UserEvent? I've never played with custom payloads in email template so just being curious
n
That (EMAIL) is not a valid execution context type.
👍 1
h
didnt know that cause I saw it in the official doc. Thanks for the info man
s
The issue is triggers. When an email is sent, yes it will render an attached PDF document, however it will not trigger the PRINT execution type. If it needs to include custom realtime/dynamic data, then the quickest solution would be creating a workflow to send the email. As part of this workflow, you would need to include a workflow action script to store the latest data as a JSON on the transaction in a long text or rich text field (noting limitations on field size etc...). Then you can ?eval the JSON data and use it normally.
If it does NOT need to be dynamic, then a User Event beforeSubmit or afterSubmit (depending on context timing and the data being extracted) script to gather the data and store on the transaction on create/edit would do the trick
h
I see. thanks for the knowledge nugget @Stefan Reeder
👍 1
s
The result of many hours of frustration then having a light bulb moment 🙂
💡 1
n
You can also add the data to a field on beforeLoad (if the data you need is available). The trick is to add the field then populate it since you cannot manipulate an existing field in beforeLoad. Note, this is not persistent, once you navigate away from the record the field disappears but there is a use case for doing it in beforeLoad.(also you could move the data to a regular field in before submit if needed).
s
Sadly not triggered when emailing a PDF
n
Ah, Ok good to know, but pertinent if you are doing other operations such as viewing / editing a record or that trigger those events.