Difference between getMessage and new GwtMessage().getMessage()

sumersingh
Giga Guru

I am going to show you what exactly the difference between getMessage and new GwtMessage().getMessage().

getMessage() and new GwtMessage().getMessage() both we use in client side to show the sys ui message. but there is some difference.

getMessage is only used for static key like getMessage("Incident resolved")

you can't use getMessage with dynamic input key parameter like

getMessage("Incident {0} resolved","INC00001")

it will not resolve "{0}".

for dynamic key input parameter you have to use

new GwtMessage().getMessage("Incident {0} resolved","INC00001")

Now you understand that if there is static key then we can use getMessage and for dynamic key input parameter we have to use new GwtMessage().getMessage.

now question comes like what if dynamic inputs are more than one then how we have to use.

 

new GwtMessage().getMessage("Incident {0} resolved, resolved date {1} and caller name {2}","INC00001","2024-20-5","Abel tutor")

we have to put all dynamic keys as string then key will resolved with associated value.

if you put all value in array then only first key will resolve with array value remaining will not resolve.

for an example

new GwtMessage().getMessage("Incident {0} resolved, resolved date {1} and caller name {2}",["INC00001","2024-20-5","Abel tutor"])

it will display Incident INC000012024-20-5Abeltutor resolved, resolved date {1} and caller name {2}

now question comes into your mind why this is happening.

why getMessage and new GwtMessage().getMessage behave differently even though under the hood getMessage is calling new GwtMessage().getMessage function. then output should be same but it is not happening here.

let's see the code how it is working under the hood.

For getMessage ServiceNow uses below code .

sumersingh_0-1716791311171.jpeg

 

if you see here in our case last line will execute and it is taking only one parameter remaining parameter it is not considering.

that's why in dynamic input key in getMessage does not resolve.

than how it is resolving in new GwtMessage().getMessage event same function is calling.

only difference is the arguments .

when we calling with getMessages then we are only supply to first parameter to new GwtMessage().getMessage function due to this new GwtMessage().getMessage()function getting only one parameter and one argument.

but when we directly calling new GwtMessage().getMessage with along our keys

then we are passing only one parameter but arguments are two here.

that's why getMessage does not resolve the dynamic input key.

code for new GwtMessage().getMessage

sumersingh_1-1716791310935.jpeg

 

if you are confusing the passing the parameter and the arguments in function

let me give you one example.

sumersingh_2-1716791310600.jpeg

 

it will give you this result.

sumersingh_3-1716791310959.jpeg

 

here sum function accept only one parameter but when we are calling it we are passing two arguments. in this if you want to get second parameter then you can use arguments it will give you all input parameter which pass while calling the function.

 

0 REPLIES 0