Is it possible to open a Knowledge Article direct to the attachment without opening the article?

Flitdog78
Giga Expert

As per the Subject - is the service able to open the pdf attached to the article directly within the viewer without downloading it?

Flitdog78_0-1691074546218.png

 

1 ACCEPTED SOLUTION

Riya Verma
Kilo Sage
Kilo Sage

HI @Flitdog78 ,

 

Hope you are doing great.

 

Yes, it is possible to open a Knowledge Article directly to the attachment without opening the article in ServiceNow.

 

1. Create a UI Action: Go to "System Definition" > "UI Actions" and create a new UI Action with the following settings:

                   Table: kb_knowledge (Knowledge Article table)

                    Action name: open_attachment_directly

                    Client: true

2. Navigate to "System Definition" > "UI Scripts" and create a new UI Script with the following code:

 

function openAttachmentDirectly() {
  var knowledgeSysID = g_form.getValue('sys_id'); // Get the current Knowledge Article's sys_id
  var attachmentSysID = g_form.getValue('u_attachment_field'); // Replace 'u_attachment_field' with the actual field name storing the attachment sys_id

  if (attachmentSysID) {
    var attachmentUrl = new GlideURL('sys_attachment.do');
    attachmentUrl.addParam('sys_id', attachmentSysID);
    attachmentUrl.addParam('sysparm_referring_url', 'kb_knowledge.do?sys_id=' + knowledgeSysID);
    window.open(attachmentUrl.getURL());
  } else {
    alert('No attachment found.');
  }
}

 

3. Add the UI Action to the form layout: Edit the form layout of the Knowledge Article table (kb_knowledge) and include the newly created UI Action, "Open Attachment Directly."

 

 
Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

View solution in original post

9 REPLIES 9

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Flitdog78 

 

Greetings!!

 

even though solution has  been accepted but i have point here, 

- What is business case of this requirement?

- Instead of coding / scripting lets try to find out is any alternative for this or not.

 

Please mark my answer helpful or solution accepted if it serves the purpose.

Regards

Atul G

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

The business requirement is that some of our staff don't like to open the download when clicking on the article.  They would prefer to go direct into the attached document.  

Dave Littlejohn
Tera Guru

We edit the kb article source to include the document viewer in an iframe:

<p><iframe src="/$viewer.do?sysparm_stack=no&amp;sysparm_sys_id=5f6493b31b6fe5149e4e8663cc4bcb6e" width="100%" height="1000" allowfullscreen="allowfullscreen"><span id="mce_12"></span></iframe></p>

Please note that you will need to update the sys_id in the code with the attachment you want to display's sys_id.

 

Hope this helps!

Have tried this Dave.  Much appreciated Sir, But I keep coming away with an empty page when I save the code in the document it's fine

Flitdog78_0-1691160881283.png

 

But afterwards it just goes blank 

Flitdog78_1-1691160930033.png

 

My guess is that HTML Sanitization is on for that field.