Add Custom Button to Execute a Suitelet

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */

define(['N/runtime', 'N/log'], (runtime, log) => {
    function beforeLoad(scriptContext) {
        try {
            const recCurrent = scriptContext.newRecord;
            const objForm = scriptContext.form;
            const stStatus = recCurrent.getValue({
                fieldId: 'status'
            });
            const stSuiteletLinkParam = runtime.getCurrentScript().getParameter({
                name: 'custscript_suiteletlink'
            });
            const suiteletURL = '"' + stSuiteletLinkParam + '"';
            if (stStatus === 'Pending Fulfillment') {
                objForm.addButton({
                    id: 'custpage_suiteletbutton',
                    label: 'Open Suitelet',
                    functionName : 'window.open(' + suiteletURL + ')',
                });
            }
        } catch(error) {
            log.error({
                title: 'beforeLoad_addButton',
                details: error.message
            });
        }
    }
    return {
        beforeLoad: beforeLoad
    };
});

Leave a comment

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