G

reetings from Knowledge12 in New Orleans! I’ll be here all week with CrossFuze Solutions so if you’re here and see me, please introduce yourself. I’m looking forward to putting a face to all of the names of people I’ve worked with remotely.

I’m working with a client currently who wants to encourage the use of their Knowledge Base to reduce their ticket load. This, of course, is a common requirement but it’s one that I don’t think is adequately addressed in ServiceNow currently. In this post I’ll share my implementation of their idea. I don’t think it’s a perfect solution, but it might give you some ideas to improve your own process and design. This post shows how you can force a KB search using the standard search dialog whenever a user creates an incident. I’ve designed the solution to work in a standard incident form and within the service catalog so that it can be applied in whatever way you create tickets in your environment.
Automatic KB Popup

When I designed this solution, my main concern was to implement a solution that would promote the Knowledge Base search without being completely annoying to the end user. Because of this, I wanted to be very careful about allowing specific control over the event trigger that produces the KB popup window. I also wanted the solution to use as much of the out-of-box behavior as possible.

The solution I decided on was to use a client script to add a ‘blur’ event to the ‘Short Description’ field. The ‘blur’ event for a field runs when the field loses focus. In the case of the ‘Short Description’ field, it runs when somebody clicks into the field, and then clicks somewhere else. I also wanted the script to only run for a new record and only to run when there was an actual value in ‘Short Description’.

From a Standard Form Field

This script can be pasted directly into any ‘onLoad’ client script on a standard form field. The only requirements are that the form includes a field named ‘short_description’ and that field uses the standard KB search icon.

‘Pop KB Search from Short Description’
Name: Pop KB Search from Short Description
Table: Incident
Type: onLoad
Script:

function onLoad() {
   //Set the maximum number of times the popup will appear
   var maxKBPop = 1;
   var kbPopCount = 0;

   //Only run for new records
   if(g_form.isNewRecord()){
      //Add an 'onBlur' event to search KB from 'short_description' field
      var field = g_form.getControl('short_description');
      Event.observe(field, 'blur', function() {
         //Only search if there is a value
         if(g_form.getValue('short_description') != ''){
            //Only pop the search one time
            if(kbPopCount < maxKBPop){
               kbPopCount++;
               customKBPopup(field.id);
            }
         }
      });
   }
}
function customKBPopup(me) {
   self.fillField = me;
   var shortElement = gel(me);
   var searchText = shortElement.value;
   var url = 'kb_find.do?sysparm_search=' + encodeURI(searchText);
   url += "&sysparm_nameofstack=kbpop";
   url += "&sysparm_kb_search_table=" + g_form.getTableName();
   url += "&sysparm_operator=IR_AND_OR_QUERY";
   popupOpenStandard(url, 'kb2task');
}

From a Service Catalog Variable

You can also force a KB search with a catalog variable (on a record producer or catalog item). The script below assumes that you’ve got a string field named ‘short_description’. Because the knowledge icon doesn’t exist for service catalog variables, the script manufactures the icon and link on the fly so that it behaves just like the standard field.

‘Pop KB Search from Short Description’
Name: Pop KB Search from Short Description
Type: onLoad
Script:

//Add an event handler to pop KB Search when focus moves from Short Description field
function onLoad() {
   //Set the maximum number of times the popup will appear
   var maxKBPop = 1;
   var kbPopCount = 0;
   var field = g_form.getControl('short_description');
   //Add the KB Search icon
   $(field.id).up(1).insert('<td><a onclick="kbPopupSC()"><img class="knowledge" title="Search Knowledge" src="images/icons/knowledge.gifx" height="16" border="0" width="16"></a></td');
   $('status.' + field.id).up('td').setAttribute('colSpan', '3');

   //Add an 'onBlur' event to search KB from 'short_description' field
   Event.observe(field, 'blur', function() {
      //Only search if there is a value
      if(g_form.getValue('short_description') != ''){
         //Only pop the search one time
         if(kbPopCount < maxKBPop){
            kbPopCount++;
            kbPopupSC();
         }
      }
   });
}

function kbPopupSC() {
   var shortElement = $(g_form.getControl('short_description').id);
   var searchText = shortElement.value;
   var url = 'kb_find.do?sysparm_search=' + encodeURIComponent(searchText);
   url += "&sysparm_operator=IR_AND_OR_QUERY";
   
   popupOpenStandard(url, "kb2task");
}
By |2018-07-09T14:59:54-05:00May 14th, 2012|Categories: Client scripts, UI macros|Tags: , , |26 Comments

Share This Story, Choose Your Platform!

About the Author:

26 Comments

  1. Catherine Haley May 14, 2012 at 8:32 am - Reply

    This is a very interesting concept. We are looking at implementing SN this year, so I’m still very new to the SN abilities. Can SN support usage tracking on the KB overall and/or the individual articles?

    • Mark Stanger May 14, 2012 at 8:47 am - Reply

      You can. I actually developed some functionality to do this while I was at ServiceNow. The features I created are now part of the KCS plugin in the product. There’s a table named ‘kb_use’ that tracks usage when a user clicks the ‘Attach to Incident’ button on a knowledge article when the article is brought up from an Incident search.

  2. Jared Romaine May 14, 2012 at 8:34 am - Reply

    This is the exact functionality we’ve been looking for. Unfortunately I added this client script (incident table, onLoad) in our test instance and it’s not popping the KB search. We have not modified the name of the short_description field or the standard KB search icon. Any suggestions?

    • Mark Stanger May 14, 2012 at 8:49 am - Reply

      You might check to see if you’ve got a popup blocker blocking something. You can also try it on a demo instance and send me the URL if you can reproduce it there.

      • Jared Romaine May 14, 2012 at 8:59 am - Reply

        Thanks for the prompt help. I added this client script to our dev instance which is basically a demo instance of SN and it is working as expected. I’m not sure how much help you can provide at this point, as it appears some of our customizations may be the reason why it’s not working in our test instance. Any other suggestions are appreciated.

        • Mark Stanger May 14, 2012 at 9:09 am - Reply

          It’s most likely caused by another client script or UI policy that is failing. Best advice I can give is to take a look at the error console in your browser and see if there’s an issue with something. If you’re at K12 I’d be happy to meet in person and see if we can figure it out together.

  3. Jared Romaine May 14, 2012 at 11:59 am - Reply

    One final question… Is there a way we can add some text to the pop-up? Something to the effect of “I have used the words you entered in the short description field to suggest the following knowledge base articles. If you do not find the answer to the issue you are reporting you can close this window and proceed with the incident submission.”

    • Mark Stanger May 14, 2012 at 12:40 pm - Reply

      It’s possible, but it would be a fairly complex UI script or UI page modification to do that. You could do a standard javascript alert box in the script I’ve given. Not as user friendly, but much simpler.

  4. Linda Cividanes May 15, 2012 at 4:15 pm - Reply

    Thank you for these client scripts. I was able to apply the first client script to our incident and it is working, but when I applied the other client script to our record producer it did not which I am not surprised since I am not that familiar with record procdures. I applied it to the actual item (Report an Incident) is that correc?

    • Mark Stanger May 15, 2012 at 4:23 pm - Reply

      The client script for a catalog item should be an ‘onLoad’ script and can be applied against the item or an associated variable set. If I were to guess what the problem is it’s probably another client script that is causing the new one to fail, or your short description variable isn’t named ‘short_description’. If you can set something up in demo to reproduce and send me a link I can take a look there.

  5. Slava Savitsky August 8, 2012 at 5:50 am - Reply

    This is a very interesting approach. And also an elegant one because it does not involve too much coding. We had a similar requirement last year, and unfortunately this post was not available at that time. So we had to develop something ourselves. Eventually we came up with what we call a “dynamic KB search” because it works in an as-you-type manner.

    As the user keys in the Short Description, a client script calls a script include that searches the knowledge base in the background and outputs the results back to the client script. The client script then displays search results inside a UI formatter that we placed just below the Short Description field.

    • Mark Stanger August 8, 2012 at 6:32 am - Reply

      I love the sound of the approach you took. I think something like that is ideal for a lot of people, but it is much more complex. I’d love to take a look at what you did if you have some code or an update set to share.

      • Slava Savitsky August 22, 2012 at 6:10 am - Reply

        I am afraid I cannot share the code due to the corporate policy but you can take a look at the screenshot I uploaded here:
        http://v-savitsky.narod.ru/dynamic_kb_search.png

        The only initial requirement was to ensure a KB search is performed every time an incident is registered by the service desk. But as we worked on the code, we decided to add some more features:

        1. Preview of articles by hovering over Info icon on the left
        2. Copying the permalink by right-clicking the article title
        3. Narrowing down the search scope by clicking on Topic, Category, Business Service, or Configuration Item
        4. Articles meant for business users are marked with a green tick icon
        5. Articles marked for review have a red flag beside the title indicating that they should be used with caution
        6. Articles related to problem records are marked with a yellow triangle, the toolip containing PRB number and status

        All of these (as well as the colors and the number of articles to display) are configured as system properties, so we can easily switch them on/off one by one if required.

        Of course our solution still needs some polishing but even the way it is now it saves our service desk agents quite a lot of time.

        • Rick Mann October 7, 2013 at 1:59 pm - Reply

          Slava – Great post. Did you write the script include yourself or were you calling one of the existing Knowledge script includes? This will be very handy for our Service Desk.

  6. Slava Savitsky December 5, 2012 at 8:25 am - Reply

    Regarding your note about the Berlin release… I think it should work fine if you use popupOpenStandard instead of kbPopup in a similar way to what you do for Service Catalog forms.

    • Josh January 14, 2013 at 11:59 am - Reply

      Slava, your idea to use popupOpenStandard worked well. I did have to modify it a bit to work by putting this inside the popupOpenStandard:

      “…sysparm_search=”+g_form.getValue(‘short_description’)+”…”

  7. Chris Devine January 24, 2013 at 4:52 pm - Reply

    Hello Mark, you could create a custom function and add it to the end of the onload script and then call it instead of kbPopup. I’ve added the line sysparm_kb_search_table to what kbPopup used to do as I can see that is what is done in Berlin (instance name/scripts/elements/element_attribute_knowledge.jsx). In Berlin it looks like encodeURIComponent is used instead of encodeURI so that could be changed in the customKbPopup too.

    function customKbPopup(me) {
       self.fillField = me;
       var shortElement = gel(me);
       var searchText = shortElement.value;
       var url = 'kb_find.do?sysparm_search=' + encodeURI(searchText);
       url += "&amp;sysparm_nameofstack=kbpop";
       url += "&amp;sysparm_kb_search_table=" + g_form.getTableName();
       url += "&amp;sysparm_operator=IR_AND_OR_QUERY";
       popupOpenStandard(url, "kb2task");
    }
    • Mark Stanger January 24, 2013 at 5:19 pm - Reply

      YES!!! Thanks for the assist Chris! I just updated the code above so that this works from standard forms again using your workaround.

  8. Adam Domanski March 26, 2014 at 6:05 am - Reply

    Hi Mark, this is a great feature and I’ve been looking at implementing it in our instance. One snag we have found, however, is that to be able to use the “Attach To Incident” button, the record needs to have been saved first. The problem here is that we’d like the immediacy of being to raise Incidents and save them as First-Time fix in one go, and of course having to save Incident first before attaching Knowledge hinders this. This behavior is understandable from a database perspective, however, as the Incident needs to be saved to be able to build a relationship to it. Have you been able to find a way around this?

    • Mark Stanger March 26, 2014 at 6:52 am - Reply

      Hey Adam, I actually haven’t found a way around that. You may need to contact ServiceNow support on that one.

  9. Sayan July 16, 2014 at 3:27 am - Reply

    Hi Mark,
    Thanks for the solution. This is exactly what I have been looking for. There is something though I was wondering if it could be done. The Search pops up in a new window. Can the search results be displayed in the same page ?

    • Mark Stanger July 16, 2014 at 6:46 am - Reply

      The search results can be displayed in the same page, but the solution to do that is much more complex, and accomplished in a completely different way. Crossfuze Solutions does that in their Knowledge Turnkey solution and it works very well. You can take a look at the Knowledge product here and request a demo if you like.

      http://www.crossfuze.com/solutions/turnkey-solutions

  10. pattipdx September 5, 2014 at 3:27 pm - Reply

    It worked like a charm! Thanks for sharing.
    @pattipdx

  11. Ketan Bhagwat April 1, 2015 at 7:07 am - Reply

    I recently came across same situation, i used below script

    function onChange(control, oldValue, newValue, isLoading, isTemplate) {
       if (isLoading || newValue == '') {
          return;
       }
       customKBPopup(newValue.toString());
    }

    function customKBPopup(searchText) {
       var url = 'kb_find.do?sysparm_search=' + encodeURI(searchText);
       url += "&amp;sysparm_nameofstack=kbpop";
       url += "&amp;sysparm_kb_search_table=" + g_form.getTableName();
       url += "&amp;sysparm_operator=IR_AND_OR_QUERY";
       popupOpenStandard(url, 'kb2task');
    }

Leave A Comment