Issues with getMessage in Service Portal Client Script

Ravish Shetty
Tera Guru

 

 


Hi everyone,

 

I’m currently facing two challenges with the getMessage function while using it in an on-submit client script for a catalog item in Service Portal. I’d appreciate your guidance on these issues.

 

Issue 1: Asynchronous Behavior of getMessage
I need to use the getMessage function on the client side to display messages to users when certain validations fail. However, since getMessage operates asynchronously in Service Portal, I am encountering issues where the function doesn’t always load the message in time.

For instance, in the following code snippet, if both validation_1_fails and validation_2_fails are true, the message variable may remain empty because the getMessage calls haven't completed before the check. As a result, the return false statement isn’t executed as expected:

 

 

 

function onSubmit() {
    var message = '';
    if (validation_1_fails) {
        getMessage('Validation 1 failure.', function(msg) {
            message += '\n' + msg;
        });
    }
    if (validation_2_fails) {
        getMessage('Validation 2 failure.', function(msg) {
            message += '\n' + msg;
        });
    }
    if (message !== '') return false;

 

 

 


To mitigate this, I’ve tried caching these messages in an on-load script, but this approach leads to duplicate code, which I’m looking to avoid. Does anyone have a more elegant solution?

 

Issue 2: Parameterized getMessage
On the server side, we can use parameterized getMessage to dynamically inject values into the message string. However, this functionality doesn’t seem to work on the client side. For example, the following code fails in the client script:

 

 

 

function onSubmit() {
    var message = '';
    if (validation_2_fails) {
        getMessage('Validation 2 failure for {0} and {1}.', ['abc', 'def'], function(msg) {
            message += '\n' + msg;
        });
    }
    if (message !== '') return false;
}

 


Is there any way to achieve parameterized messages with getMessage in Service Portal client scripts?

 

Thanks in advance for your insights and suggestions!

Ravish

1 REPLY 1

Ankur Bawiskar
Tera Patron
Tera Patron

@Ravish Shetty 

why not use 2 hidden variables and load them with the default value in variable configuration.

keep them on form and towards the end and hide

whenever your onSubmit validation runs, get the value from that variable and show

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader