SuiteScript 2.0 code snippet to store a transaction PDFs base64 value in cache memory for a fixed duration

// Let recordID be the transaction internal Id
const renderToPdf = (recordID) => {
    try {
        let returnVal = '';
        let singleFileCache = cache.getCache({
            name: 'singleFileCache',
            scope: cache.Scope.PRIVATE
        });
        let renderedPDFCache = singleFileCache.get({
            key: `singleFileCache${recordID}`
        })
        if (!renderedPDFCache) {
            let pdfFile = render.transaction({
                entityId: parseInt(recordID),
                printMode: render.PrintMode.PDF,
                inCustLocale: false
            });
            log.debug("pdfFile", pdfFile);
            let base64Content = pdfFile.getContents();
            returnVal = base64Content;
            if (Number(pdfFile?.size) < 512000) { //The maximum size of the value is 500KB.
                singleFileCache.put({
                    key: `singleFileCache${recordID}`,
                    value: base64Content,
                    ttl: 1000 // The maximum duration, in seconds, that the value may remain in the cache.
                });
            }
        } else {
            returnVal = renderedPDFCache
        }
        return `${returnVal}`;
    } catch (e) {
        log.error('Error in renderToPdf', e);
        return ""
    }
}

Leave a comment

Your email address will not be published. Required fields are marked *