Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

What is the difference between getMessage() and getMessages() in ServiceNow?

AzeemullahA
Tera Contributor

I am confused between getMessage() and getMessages() in ServiceNow.
Both are used to fetch localized messages from the System Message table, but I want to understand the exact difference and when to use each one.

Can someone briefly explain this with a simple example?

2 ACCEPTED SOLUTIONS

DiveshTyagi
Tera Guru

Hi @AzeemullahA ,

 

 

In ServiceNow, getMessage() retrieves a single localized message string, while getMessages() retrieves multiple localized messages at once. Use getMessage() when you only need one translation, and getMessages() when you want to fetch several translations in a single call for efficiency.

 

Key Differences

 

Feature getMessage() getMessages()

Purpose

Fetches one localized message

Fetches multiple localized messages

InputA single message key (string)An array of message keys
Use CaseWhen you need just one messageWhen you need several messages at once (better performance)
PerformanceMultiple calls can be inefficientMore efficient for bulk retrieval
Return TypeA single translated stringAn object/dictionary of key-value pairs (key → translated string)

 

Example 1: Using getMessage()

// Client Script
var msg = getMessage('hello_world');
alert(msg); 
// Output: "hello_world"

 

Example 2: Using getMessages()

// Client Script
var msgs = getMessages(['hello_world', 'goodbye_world']);
alert(msgs['hello_world']);   // "Hello World"
alert(msgs['goodbye_world']); // "Goodbye World"

 

 

 

 

 

------------------------------------------------------------------------------------------------------------------------------

If this helps you then mark it as helpful and accept as solution.

 

View solution in original post

Aditya40
Mega Guru

GlideSystem - getMessage(String messageID, Object args)

gs.getMessage() retrieves messages from the Messages (sys_ui_message) table based on the current user's language settings. In short, you can write language independent scripts provided the right messages are in the table. Consider the following script:

gs.addInfoMessage('Hello');

No matter what, this will always print "Hello". Now, let's go to System UI> Messages and enter the following record:

Let's modify that script slightly to use the message we just created.

var msg = gs.getMessage('Hello');
gs.addInfoMessage(msg);
var my_message = gs.getMessage("Abort adding action '{0}', same subflow can't be added twice in this subflow.", current.action.name);
alert(my_message);

GlideSystem - getMessageS(String messageID, Object args)

Retrieves translated messages to display in the UI and escapes all ticks (').

If the specified message identifier (key) exists in the Message [sys_ui_message] table for the current language, the method returns the translated message. If the specified message identifier does not exist for the current language, the method returns the English version of the message. If the message identifier does not exist in the table, then it returns the message ID.

var my_message = '${gs.getMessageS("I love France")}'; 
alert(my_message);

 

View solution in original post

5 REPLIES 5

getMessage() also has a function available that provides the same functionality like the second parameter in gs.getMessage (server-side). It can be used this way:

getMessage("Msg: {0}").withValues(["Text"])