Messages can provide important information for users. The Message module occurs most frequently in client-side scripts, but did you know that we can display these useful messages in view mode on the server-side using the “N/ui/serverWidget” module?

The Nature of Messages

The “N/ui/message” module shows real-time information to the user.

This module allows developers to manipulate the DOM through the official SuiteScript API. There are some drawbacks to this, however. It would seem that NetSuite supports this kind of DOM manipulation through Client Scripts only. Because Client Scripts run on the “client” (the user’s browser), real-time manipulation of what the user sees is quite straightforward. But such interaction with the DOM in a server-side script is not quite so simple a task.

The ui/serverWidget Module

This is where the ui/serverWidget module comes in. The serverWidget module is a mixed bag of tools that allow us to interact with the DOM through the server. This “bag” is a useful tool for handling messages in server-side scripts.

The form.addPageInitMessage() method can display a message when the page loads. All we need to do is add the “N/ui/serverWidget” and “N/ui/message” modules. After meeting certain criteria, we can display a message whenever the page loads.

function beforeLoad(context) {
    if(context.type === 'view')
        context.form.addPageInitMessage({type: message.Type.INFORMATION, message: 'This is a message in view mode', duration: 7000});
}

In the example above, a blue banner-message will display whenever someone loads a page in view mode.

A Real-World Example

There are many use cases for these kinds of messages. I will give one real-world example where the “addPageInitMessage()” method has been useful.

For one client, I needed to update the rates on an Estimate record. When I clicked a button in View mode, the script would load the record in dynamic mode, update the prices, and refresh the page. After the page refreshed, I needed to show some kind of confirmation message so that the user would know the automation was successful. I was able to accomplish this by passing a custom parameter into the URL when the page reloaded. The beforeLoad User Event script would check the URL for that custom parameter. If it existed and was set to true (“&pricerefreshed=T”), the script would display the confirmation message.

This is just one example where the “addPageInitMessage()” method can come in handy.

Conclusion

Although manipulation of the DOM in server-side scripts can be challenging, it is often possible using the right tools. We hope you find this blog helpful. Don’t forget to subscribe to our email list if you would like to receive weekly SuiteScript help in your inbox!