How to clear a specific message at the top of the form

jmweli
Kilo Expert

Hi ServiceNow Community Developers,

 

Is there a way to clear a specific message at the top of the form? Right now I am using gs.clearMessages() however the problem with this API is that clears all messages at the top of the form even the ones I don't want to clear at that point.

 

Here is an example: I have a message that gets displayed at the top of the form by an onLoad client script, then I have a boolean (true / false ) field that displays a message at the top of the form depending on its value. If checked it displays the message, if unchecked it clears the message. I am doing all this in this boolean field by means of a onChange client script. The problem is when I uncheck this boolean field it then clears all the messages at the top of the form even the one that was put there by an onLoad client scripts. Is there a way I can overcome this issue? Please advise.

 

Thanks,

Johannes

1 ACCEPTED SOLUTION

jmweli
Kilo Expert

Hi Subhajit,



Thanks for your suggestion. I managed to get this to work. Basically what I did was on the onchange client script where the gs.clearMessages() was clearing all messages out I went to the messages that were unintentionally getting cleared and checked how they were set up in the first place. I then included in my onchange client script the same condition that was setting them up and I got the desired behavior. So basically I set them back after unintentionally clearing them. In the end this was way easier than I thought.



Johannes


View solution in original post

6 REPLIES 6

Subhajit1
Giga Guru

Hi Johannes,


Kindly take a look at the following link and see if you get any ideas:-


http://wiki.servicenow.com/index.php?title=Display_Field_Messages



Thanks,


Subhajit


jmweli
Kilo Expert

Hi Subhajit,



Thanks for your suggestion. I managed to get this to work. Basically what I did was on the onchange client script where the gs.clearMessages() was clearing all messages out I went to the messages that were unintentionally getting cleared and checked how they were set up in the first place. I then included in my onchange client script the same condition that was setting them up and I got the desired behavior. So basically I set them back after unintentionally clearing them. In the end this was way easier than I thought.



Johannes


Inactive_Us1885
Giga Contributor

This is a little hackish but it works...



This will only work on Desktop versions of ServiceNow. The Mobile / Android apps will not allow manipulation of the DOM.



I had a requirement where I needed to display a loading data message to the user while I waited for an API to return.



Note: this will remove any other notification if the if statement returns true on another notification.



This is a client script that runs onLoad of the form, I am using setTimeout to simulate an API call return.



This will only work on a SCOPED application if you add the following system property to that application scope "glide.script.block.client.globals=false", this is because scoped applications by default block DOM manipulation.



function onLoad() {



  var loadingMessage = "This is a test from script";




      g_form.addErrorMessage("THIS IS AN ERROR");


      g_form.addInfoMessage(loadingMessage);




      setTimeout(function() {



              var notifications = document.getElementsByClassName("notification");


              for (var i = 0; i < notifications.length; i++) {


                      if (notifications[i].innerText == loadingMessage) {


                              notifications[i].remove();


                      }


              }


             


      }, 8000);


   


}


adamtosaki
Tera Contributor

Sorry for bumping an old thread. I was wondering about the same and found a good solution (with a bit of DOM):



$j('#myInfoMessage').remove();


g_form.addInfoMessage('Simple Message', 'myInfoMessage');



Of course you can change the ID ('#myInfoMessage' and 'myInfoMessage') to anything, and it will only clear that specific message. I used it in an OnChange client script. When the specific field changed, I immediately delete this one specific message, and I only add it back if I have a response (calling AJAX).



Please try this and mark it as the answer so others can see as well