Redirect to Suitelet page on Button click

UserEventScript – This script will add the button name ‘Prit vouchers’ in SO record under ‘bookings list’ tab.

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
/****************************************************************************
 * ENLIGHTEN SMILES
 * EN-316 ES Lab Printing Vouchers
 * **************************************************************************
 * Date: 11-08-2020
 * 
 * Author: Jobin & Jismi IT Services LLP
 * Script Description : This script will add the button name 'Prit vouchers' in SO record under 'bookings list' tab.
 * Date created : 11 August 2020
 *
 * REVISION HISTORY
 * 
 * Revision 1.0 11/08/2020 md: Create
 * 
 ****************************************************************************/
define(['N/record', 'N/ui/serverWidget'],

    function(record, serverWidget) {

        function beforeLoad(scriptContext) {
            try {

                var customer_Record = record.load({
                    type: record.Type.CUSTOMER,
                    id: scriptContext.newRecord.id
                });
                var voucherCount = customer_Record.getLineCount({
                    sublistId: 'recmachcustrecord_cust_customer'
                });
                // subtab internal ID on where you would like the button to show
                var voucherSublist = scriptContext.form.getSublist({
                    id: 'recmachcustrecord_cust_customer'
                });                
                if (voucherCount > 0) {
                    scriptContext.form.clientScriptFileId = 1161817; //Client script
                    voucherSublist.addButton({
                        id: 'custpage_button_redirect',
                        label: 'Print Vouchers',
                        functionName: 'printVouchers'
                    });
                }
            } catch (e) {
                log.debug("ERROR@BEFORELOAD", e);
                log.error("ERROR@BEFORELOAD", e);
            }
        }


        return {
            beforeLoad: beforeLoad
        };

    });

ClientScript – This script will redirect to print voucher page.

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 */
/****************************************************************************
 * ENLIGHTEN SMILES
 * EN-316 ES Lab Printing Vouchers
 * **************************************************************************
 * Date: 11-08-2020
 * 
 * Author: Jobin & Jismi IT Services LLP
 * Script Description : This script will redirect to print voucher page.
 * Date created : 11 August 2020
 *
 * REVISION HISTORY
 * 
 * Revision 1.0 11/08/2020 md: Create
 * 
 ****************************************************************************/
define(['N/currentRecord', 'N/record', 'N/url'],

    function(currentRecord, record, url) {

        function pageInit(scriptContext) {
            //SuiteScript 2.0 entry point scripts must implement one script type function.
        }

        function printVouchers() {

            try {

                var currentRec = currentRecord.get();
                var recid = currentRec.id;
                console.log("recid", recid);

                //Setting the url of the suitelet script 
                var output = url.resolveScript({
                    scriptId: 'customscript_print_vouchers',
                    deploymentId: 'customdeploy_print_vouchers',
                    returnExternalUrl: false,
                }) + '&custpage_customer_search=' + recid;

                console.log("output", output);

                window.open(output);

            } catch (e) {
                log.debug("ERROR@BEFORELOAD", e);
                log.error("ERROR@BEFORELOAD", e);
            }
        }

        return {
            pageInit: pageInit,
            printVouchers: printVouchers
        };

    });

Leave a comment

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