Get sys_id of attachment in ui action
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2024 01:45 AM
Hi, I have a button on workbench.. I want to download the attachment of the record. it is not working. Button is on workspce.
function onClick(g_form) {
var sysID = g_form.getUniqueValue();
var gr = new GlideRecord("sys_attachment");
gr.addQuery('table_sys_id', sysID);
gr.query();
while (gr.next()) {
var attSysID = gr.sys_id;
}
alert(sysID);
var URL = '/sys_attachment.do?sys_id='+attSysID;
//window.open(URL, '_blank');
//action.setRedirectURL(URL);
//action.setReturnURL(current);
top.window.open(URL, 'blank');
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2024 02:28 AM
Hi @Rosy14 the reason your getting alert as null because in Workspace attachment is stored as activity not like in native UI, hence you need to move your code to script include to check in attachment table and return sysid, l have tested the code I shared, should work for you, let me know if it doesnt
Harish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2024 02:48 AM
tried but not working for me.
UI action:
function onClick(g_form) {
var my_ga = new GlideAjax('GeAttachmentSysID');
my_ga.addParam('sysparm_name', 'getAttachment'); // pass function name used in script include
my_ga.addParam('sysparm_id', g_form.getUniqueValue());
my_ga.getXMLAnswer(function(response) {
alert(response);
if (response) {
top.window.open("/sys_attachment.do?sys_id=" + response, '_blank');
}
});
//window.open(URL, '_blank');
//action.setRedirectURL(URL);
//action.setReturnURL(current);
//top.window.open(URL, 'blank');
//g_navigation.open(URL, '_blank');
}
script include:
var GeAttachmentSysID = Class.create();
GeAttachmentSysID.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getAttachment: function() {
var sysID = this.getParameter('sysparm_id');
var gr = new GlideRecord("sys_attachment");
gr.addQuery('table_sys_id', sysID);
gr.query();
while (gr.next()) {
return gr.getValue('sys_id');
}
},
type: 'GeAttachmentSysID'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2024 02:53 AM
Hi @Rosy14 your code looks correct. the record you test does it have a attachment? can you check it?
It works fine in my instance
Harish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-08-2024 05:51 AM
Hi any update on it? its showing undefined only.
SI:
var GetAttachmentSysID = Class.create();
GetAttachmentSysID.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getAttachment: function() {
var sysID = this.getParameter('sysparm_id');
var gr = new GlideRecord("sys_attachment");
gr.addQuery('table_sys_id', sysID);
gr.query();
if (gr.next()) {
gs.addInfoMessage(gr.getValue('sys_id'));
return gr.getValue('sys_id');
}
}
});
UI action:
function onclick(g_form) {
var my_ga = new GlideAjax('GetAttachmentSysID');
my_ga.addParam('sysparm_name', 'getAttachment'); // pass function name used in script include
my_ga.addParam('sysparm_id', g_form.getUniqueValue());
var attSys = my_ga.getXMLAnswer();
alert(attSys);
// top.window.open("/sys_attachment.do?sys_id=" + attSys, '_blank');
// win.focus();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-07-2024 02:08 AM - edited ‎03-07-2024 02:09 AM
Hi @Rosy14
If there are more than 1 attachment to the record? You want to open all the attachments?
Also the URL which you are opening in new tab will download the attachment automatically which is same as clicking on attachment.
Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP