Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Show attachment in new window

Kim Kronborg
Kilo Expert

Hi,

I have a request for showing a document revision (can be a pdf) in a new window. Currently I have developed this UI action where I first find the revision for the document and then find the attachment in the sys_attachment table. But using this code will always download the document found to the users pc.

Any suggestions how I can just show the document in a new window and prevent it from downloading to the users pc ? Or is it just standard servicenow that it will always download to the users pc ?

var docnumber = current.getValue('number');

var sysid = current.sys_id.toString();

var doc_name;

var doc_rev = new GlideRecord('dms_document_revision');

doc_rev.addQuery('document', sysid);

doc_rev.orderByDesc('sys_updated_on');

doc_rev.query();

if (doc_rev.next()){

  var attach_sysid = doc_rev.getValue('sys_id');

  var doc_attach = new GlideRecord('sys_attachment');

  doc_attach.addQuery('table_sys_id', attach_sysid);

  doc_attach.setLimit(1);

  doc_attach.query();

  if(doc_attach.next()){

      var id = doc_attach.getValue('sys_id');

      gs.addInfoMessage('Document found');

      action.setRedirectURL('sys_attachment.do?sys_id=' + id);

  } else {

  gs.addInfoMessage('Found no document');

  action.setRedirectURL(current);

  }

} else {

  gs.addInfoMessage('Found no document');

  action.setRedirectURL(current);

}

1 ACCEPTED SOLUTION

Alikutty A
Tera Sage

Hi Kim,



When you say to show attachment in a new window, what do intend to display? The attachment record in sys_attachment table cannot be opened as a record, it is a default behavior that the file gets downloaded when you open it.



Thanks


Please Hit like, Helpful or Correct depending on the impact of the response


View solution in original post

7 REPLIES 7

Ujjawal Vishnoi
Mega Sage

Hi Kim,



You can use UI action with below information



Client : True


Onclick : redirect()



Script:



function redirect(){


window.open('your url','_blank');


}



Hope this helps.



Regards


Ujjawal


Hi Ujjawal,



I tried that already but couldn't get it to work.



/Kim


Midhun1
Giga Guru

Hi Kim,



I think view will work out but it will download. Which ServiceNow version are you using?


Hi Midhun,



Using Helsinki - what do you mean with view ?



/Kim