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.

Attachment Variables posted in Approval Notifications

Karen16
Giga Guru

Hi,

I read the community article for Attachment variables 'https://www.servicenow.com/community/itsm-articles/attachment-variable-making-attachment-mandatory-i... and followed the script to display an attachment variable in the Approval Notification.

 

In the Email Template for our approval notification, we already had a script to add sys_attachments for the ritms into the approval notification (works fine). 

<mail_script>
printattachments();
function printattachments() {

var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id',current.sys_id);
gr.query();
while (gr.next()) {
template.print('Attachment: <a href="http://'+gs.getProperty("instance_name")+'.service-now.com/sys_attachment.do?sys_id='
+ gr.sys_id + '">' + gr.file_name + '</a>');
}
}
</mail_script>

 

Based on the above article link, I attempted to add the script to grab the variable attachment and add it to the approval notification, but although when I test using background script to ensure that I'm grabbing the correct attachment sys_id, this script is not adding a hyperlink to the variable attachment.

var gattach = new GlideRecord('sc_item_option_mtom');
gattach.addQuery('request_item.sys_id',current.sys_id);
gattach.query();
while(gattach.next()) {
var gtype = new GlideRecord('sc_item_option');
gtype.addEncodedQuery('item_option_new.type=33^item_option_new.active=true^sys_id='+gattach.sc_item_option);
gtype.query();
while(gtype.next()) {
// gs.info('this is the value ' + gtype.item_option_new.name + ' value ' + gtype.value);
var grattach = new GlideRecord('sys_attachment');
grattach .addQuery('sys_id',gtype.value);
grattach .query();
while (grattach.next()) {
var attachLink = '<a href="' + gs.generateURL(grattach.getTableName(),grattach.sys_id) + '">' + grattach.file_name + '</a>';
template.print(attachLink + "<br />");
}
}
}

 

Any ideas what is wrong would be helpful.

Thanks,

Karen

1 REPLY 1

Voona Rohila
Mega Patron
Mega Patron

Hi @Karen16 

you can access variables in email script, You can simply use below code.

var grattach = new GlideRecord('sys_attachment');
    grattach.addQuery('sys_id',current.variables.Attchment); // current.variables.variablename
    grattach.query();
    while (grattach.next()) {
        var attachLink = '<a href="' + gs.generateURL(grattach.getTableName(), grattach.sys_id) + '">' + grattach.file_name + '</a>';
        template.print(attachLink + "<br />");
    }

 


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