- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I have a record producer field that is an image type, users are supposed to optionally add an image and it will display in an email notification sent after the record producer is submitted.
I'm having trouble with displaying the image in the email notification. Simply typing ${u_image} shows the ID ending in .iix , and I tried this script
(function runMailScript(current, template, email, email_action, event) {
var imageName = current.getValue('u_image');
if (imageName) {
var imageUrl = gs.getProperty('glide.servlet.uri') +
'sys_attachment.do?file_name=' + imageName;
template.print('<img src="' + imageUrl + '">');
}
})(current, template, email, email_action, event);and referenced it in "what it will contain" tab in email notification and it shows nothing.
Could anyone provide any insight on this?
Solved! Go to Solution.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @maryamalona ,
I reproduced a similar scenario using an Attachment-type variable in a Record Producer and a Notification Email Script.
I verified that the uploaded image was stored in sys_attachment and successfully displayed it in the email notification preview.
In my implementation, I queried sys_attachment to retrieve the image attachment's sys_id and constructed the image URL using sys_attachment.do?sys_id=. I then referenced the mail script in the notification body using ${mail_script:render_uploaded_image}.
Email Script
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
// Add your code here
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery(
'table_sys_id',
current.getUniqueValue()
);
attachment.addQuery(
'content_type',
'STARTSWITH',
'image/'
);
attachment.orderByDesc('sys_created_on');
attachment.setLimit(1);
attachment.query();
if (attachment.next()) {
var imageUrl = gs.getProperty('glide.servlet.uri') +
'sys_attachment.do?sys_id=' +
attachment.getUniqueValue();
template.print(
'<p><strong>Uploaded image:</strong></p>' +
'<img src="' + imageUrl +
'" alt="Uploaded image" ' +
'style="max-width:400px;height:auto;" />'
);
} else {
template.print(
'<p>No image attachment found.</p>'
);
}
})(current, template, email, email_action, event);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hi @maryamalona
Could you please confirm whether u_image is of type Attachment? Out-of-the-box (OOB) ServiceNow does not provide a specific variable type with image.
If it is an Attachment-type variable, you can try the following approach:
1) try with this code in your mail script
template.print("<img src=/sys_attachment.do?sys_id=" + current.variables.u_image + ">");
2) Call the mail script from Notification body:
${mail_script:name_ofYour_mail_script}
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
the variable is of type "image" in the table, and in the record producer it is of type "attachement".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @maryamalona ,
I reproduced a similar scenario using an Attachment-type variable in a Record Producer and a Notification Email Script.
I verified that the uploaded image was stored in sys_attachment and successfully displayed it in the email notification preview.
In my implementation, I queried sys_attachment to retrieve the image attachment's sys_id and constructed the image URL using sys_attachment.do?sys_id=. I then referenced the mail script in the notification body using ${mail_script:render_uploaded_image}.
Email Script
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */
email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */
event) {
// Add your code here
var attachment = new GlideRecord('sys_attachment');
attachment.addQuery(
'table_sys_id',
current.getUniqueValue()
);
attachment.addQuery(
'content_type',
'STARTSWITH',
'image/'
);
attachment.orderByDesc('sys_created_on');
attachment.setLimit(1);
attachment.query();
if (attachment.next()) {
var imageUrl = gs.getProperty('glide.servlet.uri') +
'sys_attachment.do?sys_id=' +
attachment.getUniqueValue();
template.print(
'<p><strong>Uploaded image:</strong></p>' +
'<img src="' + imageUrl +
'" alt="Uploaded image" ' +
'style="max-width:400px;height:auto;" />'
);
} else {
template.print(
'<p>No image attachment found.</p>'
);
}
})(current, template, email, email_action, event);
