2

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?

Wolfgang
  • 3,470
  • 1
  • 20
  • 36

1 Answers1

6

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,
    };
});
Wolfgang
  • 3,470
  • 1
  • 20
  • 36
  • Did you answer your own question? lol, what if you want to show it after the form is loaded? – Nadav Julius Dec 14 '22 at 13:56
  • [I certainly did](https://stackoverflow.com/help/self-answer). I’d recommend posting your question separately; it’s been a while since I did any NS development and you’ll get more eyes than just mine that way. (But TBH I'm not sure I understand the premise of your question: once the form is loaded, the server script has finished running. The only way to change the form once the client has it is with a client script.) – Wolfgang Dec 19 '22 at 01:49