Create a button to generate print from vendor return authorization

To create a button to generate print from vendor return authorization

User event script

define([],
    () => {
        /**
         * Defines the function definition that is executed before record is loaded.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @param {Form} scriptContext.form - Current form
         * @param {ServletRequest} scriptContext.request - HTTP request information sent from the browser for a client action only.
         * @since 2015.2
         */
        const beforeLoad = (scriptContext) => {
            if (scriptContext.type == scriptContext.UserEventType.VIEW) {
                let vendRmaForm = scriptContext.form;
                let vendRmaRec = scriptContext.newRecord;
                let vendRmaRecId = vendRmaRec.id;
                vendRmaForm.addButton({
                    id: "custpage_print_vendor_rma_button",
                    label: "Print Vendor RMA PDF",
                    functionName: `printVendorRma(${vendRmaRecId})`
                });
                vendRmaForm.clientScriptModulePath = "./jj_cs_vendor_rma_button_myne1146.js";
            }
        }
        return {beforeLoad}
    });

Client script

define(['N/url'],
    /**
     * @param{url} url
     */
    function (url) {
        /**
         * Function to be executed after page is initialized.
         *
         * @param {Object} scriptContext
         * @param {Record} scriptContext.currentRecord - Current form record
         * @param {string} scriptContext.mode - The mode in which the record is being accessed (create, copy, or edit)
         *
         * @since 2015.2
         */
        function pageInit(scriptContext) {


        }
        /**
         * @description print vendor rma button
         * @param {Number} vendRmaRecId vendor rma id
         */
        function printVendorRma(vendRmaRecId) {
            try {
                let parameters = {
                    vendRmaRecId: vendRmaRecId,
                }
                let printUrl = url.resolveScript({
                    scriptId: 'customscript_jj_sl_vnd_rma_prnt_myne1146',
                    deploymentId: 'customdeploy_jj_sl_vnd_rma_prnt_myne1146',
                    params: parameters,
                    returnExternalUrl: false
                });
                window.open(printUrl)
            } catch (error) {
                console.log('error @ printVendorRma', error);
            }
        }
        return {
            pageInit: pageInit,
            printVendorRma: printVendorRma
        };


    });

Suitelet script

define(['N/record', 'N/render'],
    /**
 * @param{record} record
 * @param{render} render
 */
    (record, render) => {
        /**
         * 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 reqParams = scriptContext.request.parameters;
                    let vendRmaRecId = reqParams.vendRmaRecId;
                    let renderTemp = render.create();
                    renderTemp.setTemplateByScriptId({
                        scriptId: "CUSTTMPL_JJ_VENDOR_RMA_TEMPLATE_MYNE1146"
                    });
                    renderTemp.addRecord({
                        templateName: 'record',
                        record: record.load({
                            type: 'vendorreturnauthorization',
                            id: vendRmaRecId
                        })
                    });
                    let vendorRmaPdf = renderTemp.renderAsPdf();
                    scriptContext.response.writeFile(vendorRmaPdf, true);
                }
            }
            catch (err) {
                log.error("error", err)
            }
        }
        return { onRequest }
    });

Leave a comment

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