Hide ‘Search Knowledge’ or ‘Suggestion’ Icon in ServiceNow

///Hide ‘Search Knowledge’ or ‘Suggestion’ Icon in ServiceNow

Hide ‘Search Knowledge’ or ‘Suggestion’ Icon in ServiceNow

J

ust a quick post today about something that I end up dealing with on most deployments. The requirement often comes to me as this…

How do I hide this form icon for some users but not others?

This request can apply to different types of icons, but is often asked about the ‘Search Knowledge’ (KB Search) icon or the ‘Suggestion’ icon that shows up in a default ServiceNow installation next to the ‘Short Description’ field. This article shows how you can remove this icon (or any other icon image) selectively using client scripts.

One thing to note about the ‘KB Search’ icon is that its display is actually controlled by a property on the dictionary entry of the string field it shows up next to. If you just want to remove the icon completely, you can simply remove the dictionary attribute and avoid writing a script altogether. This setting (and pretty much everything else you need to know about customizing the ‘KB Search’ functionality is described in this ServiceNow wiki page.

Here’s the script. This example is designed to be used in an ‘onLoad’ client script on the ‘Incident’ table. The main point of the script is to find the icon and hide it if the user does not have the ‘itil’ role. For a lot of HTML elements you can use a specific element ID to target and hide the element directly. In this case (and in the case of most icons in ServiceNow) the element in question doesn’t have a specific ID attribute so you have to go about your task a little bit differently.

The script goes through the entire page and finds any ‘A’ tags on the page (indicating an HTML link). Then it examines all of these elements one-by-one to see if any of them contain an ‘innerHTML’ attribute containing ‘knowledge.gifx’ — the image name of our icon. Once we’ve done that we know we’ve found our element and we can hide it with this code…

elmt.hide(); //’elmt’ refers to the found ‘A’ tag containing the icon

You can easily customize this script to hide an icon for different roles or a different icon completely by modifying the ‘g_user.hasRole’ and ‘knowledge.gifx’ sections below.

function onLoad() {
   //Hide the kb icon for users that do not have the itil role
   var isITIL = g_user.hasRole('itil'); //Check if user has itil role
   if(!isITIL){
      try{
         //Find the knowledge icon 'a' tag and hide
         $$('a').each(function(elmt) {
            if(elmt.innerHTML.indexOf('knowledge.gifx') > 0){
               elmt.hide();
            }
         });
      }catch(e){}
   }
}



If you know that the KB icon will be the only (or first) KB icon on the form, you could accomplish the same thing in one line of code…

$$('img[src="images/icons/knowledge.gifx"]')[0].hide();

Hiding the ‘Suggestion’ icon for a field

The suggestion icon is a bit simpler because it has an ID to target. Here’s a script you could use to hide the suggestion icon from the comments field if the table is not ‘incident’. The script would need to be added to an ‘onLoad’ client script on the ‘Task’ table with the ‘Inherited’ checkbox checked. Just change the ‘fld’ variable value for use with another field!

function onLoad() {
   //Identify the table and the field name
   var tbl = g_form.getTableName();
   var fld = 'comments';
   if(tbl != 'incident'){
      //Hide the suggestion icon for the field
      $('lookup.' + tbl + '.' + fld).hide();
   }
}
By | 2018-07-09T15:00:07+00:00 September 17th, 2010|Categories: Client scripts|Tags: , |5 Comments

About the Author:

Mark has worked in the IT industry since 2002 and with ServiceNow since 2007. He is the founder and creator of SN | Guru and the co-founder of Crossfuze, one of the worlds leading ServiceNow consulting partners. Prior to co-founding Crossfuze, he worked for ServiceNow as a Senior Architect on the Professional Services team. He has personally led dozens of successful implementations encompassing every part of the ServiceNow platform. He is also responsible for designing and developing groundbreaking ServiceNow solutions and best practices in the form of various applications, turnkey solutions, and integrations during his tenure at ServiceNow, Crossfuze and, of course, SN | Guru. These solutions are used today by ServiceNow administrators and consultants alike in hundreds of ServiceNow instances around the world!

5 Comments

  1. Jared Romaine November 15, 2011 at 8:05 am - Reply

    Anybody know how to take the code that executes when you click the “Search Knowledge” button and add it to the “Submit” button when users submit incidents via Self-Service? Basically we would like a popup to appear after the user clicks “Submit” that shows Knowledge articles that may apply. At the bottom of the popup we would have a “Did these suggestions resolve your issue?” message. If they choose “Yes” the incident is auto-closed, if they choose “No” the incident is created. Any suggestions?

    • Mark Stanger November 15, 2011 at 9:55 am - Reply

      Hey Jared,

      I haven’t ever seen that done that way before and I don’t have any immediate idea of how to accomplish that. Typically, if you’re doing something like this it flows from the knowledge article to the incident screen. Users search knowledge, view an article, and then determine if they still need to open an incident. That flow is built into the tool. You might consider asking this on the ServiceNow forums to see if anybody there has done something like this. I’ll reply here if I come up with anything.

      Mark

  2. Niclas September 7, 2016 at 6:20 am - Reply

    If you are on Helsinki and still using this Knowledge Icon it can not be hidden as described in this article anymore. This is because knowledge.gifx is not in the innerHTML anymore.

    However after modifying the selector you can hide the Icon:

    $$('a.icon-book:[data-type="attribute_knowledge"]').each(function(elmt) {  
               
                   elmt.hide();  
           
             });

    BTW: If you inspect the Icon with your Browser tools it will show you the following code inside the tag:

    <a title="" rel="nofollow"></a>

  3. Randy July 11, 2018 at 8:26 am - Reply

    Awesome! Worked great.

Leave A Comment