Creating the preview or download of document in netsuite.

/**

 * @NApiVersion 2.1

 * @NScriptType Suitelet

 */

/***********

 * STERI-187 SteriTek-USA-NS

 *

 * STERI-187 : Option to download the file.

 *

 *

 **************************************************************************************

 ********

 *

 * Author: Jobin and Jismi IT Services

 *

 * Date Created : 29-February-2024

 *

 * Description : This script is used to download the file.

 *

 * REVISION HISTORY

 *

 * @version 1.0 STERI-187 : 29-February-2024 : Created the initial build by JJ0177

 * **************************************************************************************

 * **/

 define([‘N/record’, ‘N/search’, ‘N/file’, ‘N/ui/serverWidget’],

 (record, search, file, serverWidget) => {

     /**

   * @param{record} record

   * @param{search} search

   * @param{file} file

   */

     /**

   * Function to check whether the field has an empty value or not.

   * @param {parameter} parameter – fieldValue

   * @returns {boolean} true – if the value is not empty

   * @returns {boolean} false – if the value is empty

   */

     function checkForParameter(parameter) {

         try {

             if (parameter != “” && parameter != null && parameter != undefined && parameter != “null” && parameter != “undefined” && parameter != ” “ && parameter != false && parameter != && parameter != ‘ ‘) {

                 return true;

             } else {

                 return false;

             }

         }

         catch (e) {

             log.debug({

                 title: “Error @ empty check Function: “, details: e.name + ‘ : ‘ + e.message

             });

         }

     }

     /**

 * Defines the Suitelet script trigger point.

 * @param {Object} scriptContext

 * @param {ServerRequest} scriptContext.request – Incoming request

 * @param {ServerResponse} scriptContext.response – Suitelet response

 * @since 2015.2

 */

     const onRequest = (scriptContext) => {

        try {

            if (scriptContext.request.method === ‘GET’) {

                let fileId = scriptContext.request.parameters.fileInternalId;

                let fileObj = file.load({

                    id: fileId

                });

                log.debug(“fileObj”, fileObj);

                let fileType = fileObj.fileType;

                if (checkForParameter(fileId)) {

                    let previewForm = serverWidget.createForm({ title: ‘Click here to Preview Document’ });

                    let downloadForm = serverWidget.createForm({ title: ‘Click here to Download Document’ });

                    // Set the preview button for applicable file types

                    if (fileType === ‘PNGIMAGE’ || fileType === ‘GIFIMAGE’ || fileType === ‘JPEGIMAGE’ || fileType === ‘PDFFILE’ || fileType === ‘PNG’ || fileType === ‘GIF’ || fileType === ‘JPEG’ || fileType === ‘PDF’ || fileType === ‘JPGIMAGE’) {

                        previewForm.addSubmitButton({ label: ‘Preview’ });

                       // previewForm.clientScriptFileId = ‘customscript_preview_script’; // Replace with the actual script ID for preview action

                    }

                    // Set the download button for all file types

                    downloadForm.addSubmitButton({ label: ‘Download’ });

                   // downloadForm.clientScriptFileId = ‘customscript_download_script’; // Replace with the actual script ID for download action

                    // Write the forms based on the file type

                    if (fileType === ‘PNGIMAGE’ || fileType === ‘GIFIMAGE’ || fileType === ‘JPEGIMAGE’ || fileType === ‘PDFFILE’ || fileType === ‘PNG’ || fileType === ‘GIF’ || fileType === ‘JPEG’ || fileType === ‘PDF’ || fileType === ‘JPGIMAGE’) {

                        scriptContext.response.writePage(previewForm);

                    } else {

                        scriptContext.response.writePage(downloadForm);

                    }

                } else {

                    scriptContext.response.write(‘Error: Please choose a file for preview or download.’);

                }

            } else if (scriptContext.request.method === ‘POST’) {

                let requestBody = scriptContext.request;

                let queryString = requestBody.parameters.entryformquerystring;

                const match = queryString.match(/fileInternalId=(d+)/);

                let fileInternalId;

                if (match && match[1]) {

                    fileInternalId = match[1];

                }

                log.debug(“fileInternalId”, fileInternalId);

                if (checkForParameter(fileInternalId)) {

                    try {

                        let fileObj = file.load({

                            id: fileInternalId

                        });

                        log.debug(“fileObj”, fileObj);

                        let fileContent = fileObj.getContents();

                        scriptContext.response.writeFile({

                            file: fileObj,

                            isInline: true

                        });

                    } catch (e) {

                        log.error({

                            title: ‘Error’,

                            details: e.toString()

                        });

                        scriptContext.response.write({

                            output: ‘Error: Unable to retrieve the file.’

                        });

                    }

                } else {

                    scriptContext.response.write({

                        output: ‘Error: Please choose a file for preview or download.’

                    });

                }

            }

        } catch (err) {

            log.error({ title: “Error at main code”, details: err });

            scriptContext.response.write({

                output: ‘Error: Please choose a file for preview or download.’

            });

        }

    };

    return { onRequest };

});

Leave a comment

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