- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2024 09:36 AM
Hi everyone,
I'm working on a catalog item in ServiceNow and I've created a variable of the Attachment type. I'd like to automatically populate this attachment with a file from a previously selected record . For example, after selecting a record, other related variables populate automatically using a Script Include, which works fine. However, I'm struggling to do the same for the attachment.
Could anyone advise on what I might be missing in my Script Include or suggest another approach to auto-populate the attachment?
Script Include:
fetchAttachment: function() {
var landscapeSysId = this.getParameter('sysparm_landscape_sys_id');
var attachmentSysId = "";
var grAttachment = new GlideRecord('sys_attachment');
grAttachment.addQuery('table_name', 'x_abc');
grAttachment.addQuery('table_sys_id', landscapeSysId);
grAttachment.query();
if (grAttachment.next()) {
attachmentSysId = grAttachment.sys_id.toString(); // Assuming you want the sys_id of the found attachment
}
if (attachmentSysId) {
var attachment = new GlideSysAttachment().get(attachmentSysId);
return attachment;
} else {
return null; // Handle case where no attachment is found
}
Thanks in advance!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2024 09:40 AM - edited ‎07-30-2024 09:47 AM
Hi @Rahul86
I faced a similar requirement in my project. Here is the corrected version of the Script Include that auto-populates the attachment field:
fetchAttachment: function() {
var SysId = this.getParameter('<sys_id of record>');
var attachmentSysId = "";
var grAttachment = new GlideRecord('sys_attachment');
grAttachment.addQuery('table_name', '<table_name');
grAttachment.addQuery('table_sys_id', SysId);
grAttachment.query();
if (grAttachment.next()) {
attachmentSysId = grAttachment.sys_id.toString();
}
return attachmentSysId;
}
As you can see, it's not necessary to return the attachment itself to your client script. Instead, you can simply return the sys_id of the attachment and set it in your variable, like I did below:
//Client Script
/* GlideAjax call to send sys_id of record
*/
/*Retrieve the sys_id of attachment
*/
function populateAttachment(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('<attachment field name>',answer);
}
This should help you achieve the desired functionality.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2024 09:40 AM - edited ‎07-30-2024 09:47 AM
Hi @Rahul86
I faced a similar requirement in my project. Here is the corrected version of the Script Include that auto-populates the attachment field:
fetchAttachment: function() {
var SysId = this.getParameter('<sys_id of record>');
var attachmentSysId = "";
var grAttachment = new GlideRecord('sys_attachment');
grAttachment.addQuery('table_name', '<table_name');
grAttachment.addQuery('table_sys_id', SysId);
grAttachment.query();
if (grAttachment.next()) {
attachmentSysId = grAttachment.sys_id.toString();
}
return attachmentSysId;
}
As you can see, it's not necessary to return the attachment itself to your client script. Instead, you can simply return the sys_id of the attachment and set it in your variable, like I did below:
//Client Script
/* GlideAjax call to send sys_id of record
*/
/*Retrieve the sys_id of attachment
*/
function populateAttachment(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('<attachment field name>',answer);
}
This should help you achieve the desired functionality.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2024 10:10 AM
Thanks @AMan1997 !! , Now its working as expected.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-30-2024 10:11 AM
If my response helped, please mark the answer helpful/correct based on impact!
Thanks,