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
Mega 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

4 REPLIES 4

Brian Lancaster
Kilo Patron

I just looked on the Developer site and compared the description of each one. The only difference I found was for getMessages it also had "Useful if you are inserting into a JavaScript expression from Jelly." So it seems like getMessages is to be used on UI Macros and UI Pages where you use Jelly script while getMessage is for things like business rule etc.

DiveshTyagi
Mega 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.

 

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);

 

its_SumitNow
Kilo Sage

Hi @AzeemullahA 

Key Difference Between getMessage() and getMessages():

getMessage() - Returns a single message as a string

  • Use when you need one specific message
  • Takes one parameter: the message key
  • Returns the localized message text directly

getMessages() - Returns multiple messages as an object

  • Use when you need several messages at once
  • Can take multiple message keys as parameters
  • Returns an object with key-value pairs
// getMessage() - Single message
var greeting = gs.getMessage("Welcome to the portal");
gs.info(greeting); // Output: "Welcome to the portal" (or localized version)

// getMessages() - Multiple messages
var messages = gs.getMessages(["Login successful", "Invalid credentials", "Session timeout"]);
gs.info(messages["Login successful"]); // Access specific message from the object
gs.info(messages["Invalid credentials"]);​

When to use each:

  • Use getMessage() when you need just one message for immediate display or logging
  • Use getMessages() when you're building UI components or need to cache multiple messages for efficiency (reduces database calls)

if my response helped, kindly mark helpful & Accept as Solution, you can accept multiple 🙂

Warm Regards

Sumit

Technical Consultant