USER EVENT SCRIPT TO HIDE THE BODY FIELDS IN ITEM RECORD

This is a user event script to hide the body fields for specific employee’s roles and to hide the custom view using jquery in before load

/**
 * @NApiVersion 2.1
 * @NScriptType UserEventScript
 */
define(['N/record', 'N/ui/serverWidget', 'N/runtime'],
    /**
     * @param {record} record
     * @param {serverWidget} serverWidget
     * @param {runtime} runtime
     */
    function(record, serverWidget, runtime) {
        
        /**
         * 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
         */
        function beforeLoad(scriptContext) {
            try {
                let currentRole = runtime.getCurrentUser().role;
                log.debug('currentRole',currentRole)
                const rolesToHideFields = ['1101','1102'];
                if (rolesToHideFields.includes(currentRole.toString())) {
                if (scriptContext.type === scriptContext.UserEventType.VIEW || scriptContext.type === scriptContext.UserEventType.EDIT) {

                    let hideFld = scriptContext.form.addField({
                        id: 'custpage_hide_buttons',
                        label: 'not shown - hidden',
                        type: serverWidget.FieldType.INLINEHTML
                    });
                    let scr = "";
     
                    scr += 'jQuery("#inpt_searchid1").hide();';
                    scr += 'jQuery("#customize").hide();';
                    
                     hideFld.defaultValue = "<script>jQuery(function($){require([], function(){" + scr + ";})})</script>"
                    // Array of field IDs to be hidden
                    var fieldsToHide = ['averagecostunits', 'totalvalue', 'searchid'];
                    for (var i = 0; i < fieldsToHide.length; i++) {
                        var field = scriptContext.form.getField({ id: fieldsToHide[i] });
                        if (field) {
                            field.updateDisplayType({
                                displayType: serverWidget.FieldDisplayType.HIDDEN
                            });
                            log.debug('fieldsToHide',field)
                        }
                    }
                }
            }
            } catch (e) {
                log.error('Error in beforeLoad', e.toString());
            }
        }

        return {
            beforeLoad: beforeLoad
        };
    });

Leave a comment

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