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

Sagar Pagar
Tera Patron

Hi @sparkles,

 

Try this updated scripts with clearMessages() method. it will remove the messages if condition is not met.
 
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);
   }else{
          g_form.clearMessages();
   }
 ------------
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);
}else{
          g_form.clearMessages();
   }
 
If my response helps you resolve your issue. Kindly mark it as helpful & correct. It will be helpful to future readers! 👍🏻
Thanks,
Sagar Pagar

 

 

 

 

The world works with ServiceNow

Hi Sagar,

Thanks for your reply, that  made the message for the second option no longer shows

Hi @sparkles,

 

Apologies for the confusion. Rather than creating two separate client scripts for the same field/variable, you can consolidate them into a single client script, which will fulfill your requirements.

 

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);
   } else if (newValue == 'second'){
        g_form.addInfoMessage('Please download, complete and attach "XXX ' + link);
  }  else{
          g_form.clearMessages();
   }

 

 

If my response helps you resolve your issue. Kindly mark it as helpful & correct. It will be helpful to future readers! 👍🏻
Thanks,
Sagar Pagar

The world works with ServiceNow

sparkles
Tera Contributor

Hi Sagar,

This works only if I select other options. But if I select "first" then change it to "second" the message from the first will remain and the message from the second will show (see attached)