Is there a way to show the results of a saved sear...
# advancedpdf
l
Is there a way to show the results of a saved search to an invoice pdf template?
s
To my knowledge, the only way to accomplish this is via scripting. We also have usage-based billing and for a period of time we used to append a saved search detailing usage at the end of the invoice. We used a custom suitelet script to generate the combined pdf. In general, though, I don’t like turning billing systems into reporting systems, and feel that customers should be able to use their normal tools within a service or application to see their usage, whenever possible. We no longer append our usage because our customers now have the ability to see it themselves directly.
l
Thank you. By the way, ours is time-based billing (based on charge rules), so we would like to show, hopefully, the time entry or charge details such as the employee, task, memo and no. of hours.
s
Right, I understand. If Netsuite is the only source of the time entries, then it does make sense to pursue a custom pdf like this. The downside is that you will need to maintain a custom-scripted solution going forward.
the basic approach in code is to use N/render and create a new render, set the template, then add both the invoice record and search results, and finally, render it as a PDF:
Copy code
let renderer = render.create();
    renderer.templateContent = template;
    renderer.addRecord({ templateName: 'record', record: invoiceRecord });
    renderer.addSearchResults({ templateName: 'chargesearch', searchResult: chargeSearchResults });
    const invoicePdf = renderer.renderAsPdf();
you’ll then need a PDF set freemarker template, combining your standard invoice PDF with the saved search PDF code
pdfInvoiceWithCharges_ftl.xml
Just note that whatever string you send as the templateName in the code is the variable name that will be used in the template. In this case, the invoice is named
'record'
and the saved search results are named
'chargesearch'
l
So, I'm just literally combining an existing PDF template and the saved search results pdf? So, I don't need to add more code because it's already defined in the invoice pdf and in the saved search?
s
right, the pdfset is combining two pdf into one (note that the pagination resets back to 1 with the second pdf)
you don’t have to use a pdfset, you can combine both into a single pdf, and can intermix data between the invoice and result set however you want, but doing it this way is just a simple way to combine the two
l
Thanks a lot. I'll give it a try.
Sorry, I'm not very techy. But is it something like this? /** * @NApiVersion 2.x * @NScriptType Suitelet */ /** * This function generates a PDF for an invoice record and includes data from a saved search * * @param {number} invoiceId - The ID of the invoice record to generate the PDF for * @param {number} searchId - The ID of the saved search to retrieve data from * @returns {file.File} - The PDF file object */ define(['N/render', 'N/search'], function(render, search) { function generateInvoicePdf(invoiceId, searchId) { // Load the invoice record const invoiceRecord = record.load({ type: record.Type.INVOICE, id: invoiceId }); // Run the saved search to retrieve charge data for the invoice const chargeSearch = search.load({ id: '26912' }); // Add a filter to the saved search to limit results to those related to the invoice record chargeSearch.filters.push({ name: 'internalid', operator: 'anyof', values: [invoiceId] }); // Execute the saved search and retrieve the results const chargeSearchResults = chargeSearch.run().getRange({ start: 0, end: 1000 // limit the number of results returned to 1000 }); // Load the PDF template const template = file.load({ id: '234', // replace with the ID of your PDF template file isDynamic: true }).getContents(); // Create a new renderer and set the template content const renderer = render.create(); renderer.templateContent = template; // Add the invoice record and search results to the renderer renderer.addRecord({ templateName: 'record', record: invoiceRecord }); renderer.addSearchResults({ templateName: 'chargesearch', searchResult: chargeSearchResults }); // Generate the PDF and return the file object const invoicePdf = renderer.renderAsPdf(); return invoicePdf; } return { onRequest: generateInvoicePdf }; });
And is it better to use client script or user event script to add the buttpn to trigger the suitelet?
s
You’ll need to use a User Event script with an attached client script. The UE will create the button itself and have the click action to call a function, while the attached client script will have the function needed to call the PDF Suitelet.
l
I will try that. Thank you!