October 23, 2012, 1:44 pm - James Farrer
Right now I'm working with a client that has to support two different languages in their ServiceNow instance. Because of this I have been thinking more about using Messages. One area in particular that is relatively new and not often understood is Client Script Messages.
Until late last year javascript alerts and other info or error messages used in client scripts were difficult to translate. Now there is a new field on the Client Script form called Messages. When I first saw this it didn't make a lot of sense to me, but after having used it a little I'm getting to be a big fan. Any time content can be separated from function it typically means less maintenance for the code.
The Messages field provides an easy way to access the System UI -> Messages table in Client Script. On the server side we've been able to do this by calling gs.getMessage("sample message"). Now in a few easy steps in a client script you can do it easily there too.
To set it up you put the key for any message that needs to be displayed in the script into the Messages field (one per line) and then when the form is loading those messages are looked up in the proper language in the Messages table and sent to the client along with the script so they are available. Then in your client script you can just call getMessage('message_key') to use the message.
If you don't want to mess with the Messages table while you're scripting then you can just put your message in as the parameter to getMessage and it will return the text as entered if it doesn't find anything in the Messages table that matches. I prefer putting a more descriptive name instead of the actual text but either way works.