Search code examples
netsuiteserver-sidesuitescript2.0

How do I render an N/ui/message banner into a form from the server side?


I have a suitelet which displays a form created with N/ui/serverWidget, and I'd like to display a message at the top of the form. However, all of the N/ui/message documentation says that it is for client-side scripting only. Is there a way to display such a banner without building a separate client script just to do it?


Solution

  • Yes, use the Form.addPageInitMessage() function. It allows you to pass in either a Message or the same options as message.create(), and will display it when the form loads.

    Here's a complete example:

    /**
     * Example of how to use Form.addPageInitMessage() to display a notice
     * on a server-rendered form.
     * @NApiVersion 2.x
     * @NScriptType Suitelet
     */
    define(['N/ui/serverWidget', 'N/message'], function(ui, message) {
        function onRequest(context) {
            var form = ui.createForm({
                title: 'Example Form',
            });
            form.addSubmitButton({label: 'Submit'});
            form.addField({
                id: 'input',
                type: ui.FieldType.TEXT,
                label: 'Input',
            });
            form.addPageInitMessage({
                type: message.Type.INFORMATION,
                title: 'Message!',
                message: 'A wild message appears!',
            });
            context.response.writePage(form);
        }
    
        return {
            onRequest: onRequest,
        };
    });