Clear infomessage

sparkles
Tera Contributor

Hi,

I have 2 client scripts on the same field, each one will display specific message based on the selection. I need to clear the message if selection changed. But when I change the option the old message still there (see screenshot).

This field has 5 options 2 of them should display a message

 
=================================================
the script is on-change
var _link = '<a href =" https://testlink.com/sites/xxxx" target="_blank">Form.</a>';
    // add the form message, using strings and the link variable that was built above
    if (newValue == 'first')
        g_form.addInfoMessage('Please download, complete and attach "XXX ' + link);
 ------------
second script
 
var _link = '<a href =" https://testlink.com/sites/xxxx" target="_blank">Form.</a>';
    // add the form message, using strings and the link variable that was built above
    if (newValue == 'second')
        g_form.addInfoMessage('Please download, complete and attach "XXX ' + link);
 
1 ACCEPTED SOLUTION

Amit Gujarathi
Giga Sage
Giga Sage

HI @sparkles ,
I trust you are doing great.
It seems like the issue you're encountering is due to the persistence of the info message when the selection changes. In ServiceNow, when you use g_form.addInfoMessage(), it adds a message to the form but does not automatically clear previous messages when the field value changes.

First script:

var _link = '<a href ="https://testlink.com/sites/xxxx" target="_blank">Form.</a>';

// Clear existing messages before adding a new one
g_form.clearMessages();

// Add the form message for the 'first' option
if (newValue == 'first') {
    g_form.addInfoMessage('Please download, complete and attach "XXX ' + _link);
}

 

Second Script

var _link = '<a href ="https://testlink.com/sites/xxxx" target="_blank">Form.</a>';

// Clear existing messages before adding a new one
g_form.clearMessages();

// Add the form message for the 'second' option
if (newValue == 'second') {
    g_form.addInfoMessage('Please download, complete and attach "XXX ' + _link);
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

6 REPLIES 6

Amit Gujarathi
Giga Sage
Giga Sage

HI @sparkles ,
I trust you are doing great.
It seems like the issue you're encountering is due to the persistence of the info message when the selection changes. In ServiceNow, when you use g_form.addInfoMessage(), it adds a message to the form but does not automatically clear previous messages when the field value changes.

First script:

var _link = '<a href ="https://testlink.com/sites/xxxx" target="_blank">Form.</a>';

// Clear existing messages before adding a new one
g_form.clearMessages();

// Add the form message for the 'first' option
if (newValue == 'first') {
    g_form.addInfoMessage('Please download, complete and attach "XXX ' + _link);
}

 

Second Script

var _link = '<a href ="https://testlink.com/sites/xxxx" target="_blank">Form.</a>';

// Clear existing messages before adding a new one
g_form.clearMessages();

// Add the form message for the 'second' option
if (newValue == 'second') {
    g_form.addInfoMessage('Please download, complete and attach "XXX ' + _link);
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Thanks Amit, it works!