Page loading using a custom button

Need to load a page by clicking a custom button from a record, without using a separate client script to write the function.

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(['N/record', 'N/search','N/ui/serverWidget','N/config'],
    /**
 * @param{record} record
 * @param{search} search
 */
    (record, search,serverWidget,config) => {
        /**
         * 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 Record_Type={'prospect': 'custrecord_jj_deal_pipeline','job': 'custrecord_jj_prj_parent'}
        const Form_ID={
            'job': 172,
            'prospect': 171
        }
        const beforeLoad = (scriptContext) => {
            try{

                if(scriptContext.type == "view" && Record_Type.hasOwnProperty(scriptContext.newRecord.type)){
                   
                    let configObj= config.load({
                        type: config.Type.COMPANY_INFORMATION
                    });

                    let custpage_request_atpl_252 =  scriptContext.form.addField({
                        id: 'custpage_request_atpl_252',
                        type: serverWidget.FieldType.INLINEHTML,
                        label: 'custpage_request_atpl_252'
                    });
                    let url =configObj.getValue({fieldId: 'appurl'})+"/app/common/custom/custrecordentry.nl?rectype=599&cf="
                         url+= Form_ID[scriptContext.newRecord.type]+'&record.'+Record_Type[scriptContext.newRecord.type]+'='+scriptContext.newRecord.id;
                        custpage_request_atpl_252.defaultValue += `<script>
                         window.custpage_request_atpl_252 = function(){
                            window.open("${url}");
                        }
                        </script>`;
               
                    
                    scriptContext.form.addButton({
                        id: 'custpage_jj_request_button_atpl_252',
                        label: 'Request Resources',
                        functionName: 'window.custpage_request_atpl_252'
                    });
                    
                }
            }
            catch(e){
                log.error("error@beforeLoad",e)
            }
        }

        /**
         * Defines the function definition that is executed before record is submitted.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @since 2015.2
         */
        const beforeSubmit = (scriptContext) => {

        }

        /**
         * Defines the function definition that is executed after record is submitted.
         * @param {Object} scriptContext
         * @param {Record} scriptContext.newRecord - New record
         * @param {Record} scriptContext.oldRecord - Old record
         * @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
         * @since 2015.2
         */
        const afterSubmit = (scriptContext) => {

        }

        return {beforeLoad, beforeSubmit, afterSubmit}

    });

Leave a comment

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