Add new button to Knowledge view

wchaiwimol
Giga Contributor

Is it possible to add a new button for example Create HR Case to the knowledge view? The functionality will be similar to Create Incident. Thanks in advance for your help.

find_real_file.png

1 ACCEPTED SOLUTION

Doing some Developper console exploring, I found that the script that handles the createIncident button reside in the UI Script "KBViewArticle"



So you have: $j('#createIncident').click(this.incidentBtnHandler.bind(this));which handles the click of the button



Then the incidentBtnHandler function uses $j('#createIncidentLinkLocation').val(); to set the location



The #createIncidentLinkLocation refers to an hidden input field which is contained inside "kb_view_hidden_fields" UI Macro.



This createIncidentLinkLocation is based on the System property (sys_properties.list in SNOW navigator). This link looks a lot like what I suggested which is: "incident.do?sys_id=-1&sysparm_query=active=true^comments=(Created after Knowledge search: $[HTML:knowledgeRecord.short_description])&sysparm_stack=knowledge_home_launcher.do"



The important part in the link is the "$[HTML:knowledgeRecord.short_description]" part. Inside "kb_..." UI Macro you have access to knowledgeRecord jelly object which contains all your knowledge fields which you may want to use for your predefined values.You usually can use it in first phase ${knowledgeRecord.field} or in second phase $[knowledgeRecord.field]



So if you want to create your button the same way the create incident is done you would need to customize:



  • 3 UI Macro (kb_view_common_header_toolbar & kb_view_hidden_fields & kb_view_common in which the variable showCreateIncident is initialized based on the fact that the knowledge is in a popup or not)
  • 1 UI Script (KBViewArticle)
  • Insert a new system property


This has the pro of respecting the actual design but have the con of making a lot of modifications that can be harder to maintain than simply putting a onclick on your button.



Depending on your level of expertise in ServiceNow and with Jelly, I think I gave you enough info to do it yourself, but if you need more help, just ask.


View solution in original post

33 REPLIES 33

@Laurent Chicoine ,

Hello, can you please provide the sample code to create Request on sc_request which will be carrying the field values such as (short description, catalog item, etc) to the request.

Bhupinder1
Giga Expert

Yes, the Print button is added to the KB article as below-


find_real_file.png



When I hit the print button it opens the KB Article in new window with "&sysparm_media=print" parameter as below-


find_real_file.png



But after this it does not automatically opens the print button.   I added the alert in script tag but no message was displayed.


I just looked into our code and found out that we had to change it following our upgrade from Fuji to Helsinki.



What we have done is to put the custom print button of "kb_view_common_header_toolbar" inside the new <j2:if test=...> that is there OOTB and to add the script inside a custom <j2:if test=...> at the end of the script. I've ommited the main part of the code to reduce the length of the code posted, but I could put it as a whole if you need it.



Here is what it looks like:



<?xml version="1.0" encoding="utf-8" ?>


<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">


      <!--A Heisenberg page header as specified in our style guide-->


      <g:evaluate var="jvar_is_kb_languages_new_ui" jelly='true'>


              var isActive = GlidePluginManager.isActive('com.glideapp.knowledge.i18n2');


              isActive;


      </g:evaluate>


      <j2:if test="$[sysparm_media != 'print']">


      <div class="navbar navbar-default snc-article-header-toolbar" role="navigation">


            ...



                              <j:if test="${showUpdateAction}">


                                      <button id="editArticle" title="${gs.getMessage('Edit content')}" class="btn btn-default navbar-btn snc-article-header-toolbar-button">${gs.getMessage("Edit")}</button>


                              </j:if>


                              <!-- Custom added to add a print Button when there is the required sysparm -->


                              <button id="printArticle" title="${gs.getMessage('Print Article')}" class="btn navbar-btn btn-primary" style="display:inline-block;" onclick="window.open('/kb_view.do?sys_kb_id=${knowledgeRecord.sys_id}&amp;sysparm_media=print', 'Printer_friendly_format', 'resizable=yes,scrollbars=yes,status=yes,toolbar=no,menubar=yes,location=no')">${gs.getMessage("Print")}</button>


                      </div>


              </j:if>


      </div>



      ...


      </j2:if>


     


      <!-- Automatically opens the print option of the browser when the sysparm get passed using the previous button -->


      <j2:if test="$[sysparm_media == 'print']">


              <script>


                      window.print();


              </script>


      </j2:if>


</j:jelly>


Hello Laurent,



It did the trick and it's working. Thanks a lot for help !!


Now, I would try that on ServicePortal



Thanks


Bhupinder


vaibhavgoel
Kilo Contributor
Hi Laurent , Is it possible  to add a new button like " MAIL" button available at all form headers  to the knowledge view? The functionality will be similar to it only. Thanks in advance for your help.
find_real_file.png