Second Parameter of getMessage() on Client Side

Mike McCall
Giga Guru

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

1 ACCEPTED SOLUTION

sergiu_panaite
ServiceNow Employee
ServiceNow Employee

Hello Michael,



getMessage(String) on client side uses only one parameter vs gs.getMessage(String, Object) that can use a second parameter.



Regards,


Sergiu


View solution in original post

15 REPLIES 15

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


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


I agree.   There could be numerous use cases for the same functionality client-side.


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

surajp
Mega Guru

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