- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-29-2015 02:58 PM
I was recently inspired by Chuck Tomasi's post on gs.getMessage() - The Second Parameter, but when I tried to add a second parameter to the client-side getMessage() function, it didn't seem to work.
Does anyone know if the client-side getMessage() function does not accept a second parameter like the server-side gs.getMessage() does?
(If it matters, we're currently on Eureka Patch 7.)
Solved! Go to Solution.
- 26,607 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-30-2015 02:50 AM
Hello Michael,
getMessage(String) on client side uses only one parameter vs gs.getMessage(String, Object) that can use a second parameter.
Regards,
Sergiu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2015 11:47 PM
Hi Michael,
To be honest, I don't know the exact reason behind limiting the client-side function in this way. But, if I think how ServiceNow platform works, everything is a record in a database, so as soon as you need that object from the database you need to do a server-side call, for which we know the solution: gs.getMessage(String, Object). On the other hand, what would be the advantage of multiple arguments to client-side function without doing a server-call? Can't think of many, at least not for this type of function.
Regards,
Sergiu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2015 09:21 AM
I was imagining a reusable message for date range fields. In my Messages, I'd have a record with a Key value of 'Date range - before' and Message value of "{0} must be before {1}."
Then, in my Client Scripts, I could do something like this:
var msgArray = [];
msgArray.push(g_form.getLabelOf('start_date'));
msgArray.push(g_form.getLabelOf('end_date'));
alert(getMessage('Date range - before', msgArray)); //...except the second parameter isn't accepted
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-20-2016 09:50 PM
I agree. There could be numerous use cases for the same functionality client-side.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-21-2019 06:01 AM
Even this is a quite old post: While playing around I stumbled across the GwtMessage() function. With that function I was able to retrieve my localized message and also replacing the parameters:
Message Key = {0} must be after {1}
Message = {0} must be after {1}
Call in Client Script:
g_form.showFieldMsg('end_date', new GwtMessage().getMessage("{0} must be after {1}", g_form.getLabelOf("end_date"), g_form.getLabelOf("start_date")),'error');
According to this the function seems not to be documented and should not be used. But even in Madrid it's still used in a couple of ootb code.
HTH
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-09-2020 05:57 AM
Hi All,
There is a way to pass variables to placeholders in the message within getMessage.
Use the withValues function on object returned by getMessage.
Please check the below code.
// Client script example:
function onLoad() {
// get preloaded message
var myMsg = getMessage("Logout"); // Logout
// get preloaded messsage and format
var myMsg = getMessage("The {0} says {1}!");
myMsg = myMsg.withValues(['cow', 'moo']); // The cow says moo!
// get message async
getMessage("Hello World", function(msg) { g_form.addInfoMessage(msg); } ); // Hello World
// get message async and format
getMessage("Good Morning, {0}!", function(msg) { g_form.addInfoMessage(msg.withValues([g_user.getFullName()])); } ); // Good Morning, Fred Luddy!
}
Regards,
Suraj