Embedding image attachments in the body of Notific... - ServiceNow Community
Allen Andreas
Administrator
Administrator

Hi,

Recently someone had asked a question on the forums that grabbed my curiosity, so I decided to dig in and check some things out. Their question was essentially: how to take an attachment variable on a catalog item and embed that attachment within a notification body. This is different than just using the "Include Attachments" checkbox on the notification as that would just include all record attachments as an attachment to the email, but not actually embedding it in the body.

So, I did some quick testing and came up with a few use case examples where this solution could be utilized. This basically involves creating a mail script and then utilizing that properly within your notification.

Option 1: Embed an image attachment from an attachment type variable in the body of a notification:

  1. Create a mail script using example code:
    template.print("<img src=/sys_attachment.do?sys_id=" + current.variables.name_of_attachment_variable + ">");

    find_real_file.png

    You can change current.variables to just current if the attachment type field is actually on your table instead of a catalog item

  2. Within your notification Message HTML field, you'd call the mail script for example:
    Here's your embedded image:
    
    ${mail_script:name_of_mail_script}​

    find_real_file.png

  3. The final result would look something like this:
    find_real_file.png

 

Option 2: Embed one or more image attachments that are directly attached to a record in the body of a notification:

  1. Create a mail script using example code:
    var gr = new GlideRecord('sys_attachment');
        gr.addQuery('table_sys_id', current.sys_id);
        gr.addQuery('content_type', "CONTAINS", "image"); //This line helps ensure you're only grabbing image type attachments
        gr.query();
        while (gr.next()) {
            template.print("<img src=/sys_attachment.do?sys_id=" + gr.getValue('sys_id') + ">");
        }​

    find_real_file.png

  2. Follow step 2 from Option 1, above
  3. The final result would look something like this:
    find_real_file.png

 

There's always more you can do (i.e., adding height="x" and width="y" as well in the img src line to control the height and width of the image in the notification), but hopefully this helps guide you if this is something you're looking to accomplish.

Please be sure to bookmark this article as well as mark it as Helpful if you thought it was helpful.

Interested in other articles I've written? Check out this Delta Exam Guide article!

Thanks and take care! 🙂

Comments
Brian186
Tera Expert

Hello @Allen Andreas 

I was searching on how to show an image attachments in the body of notification and found your link. 

 

This is exactly what I was looking.  I tested and it is working wonderful for our need. However, attached images were showing a little big in the email body.  I could not figure it out how to change the "Width" and "Height" of the image.

 

I tried this, but it is not working.  Could you please help?  Thank you

 

template.print("<img src=/sys_attachment.do?sys_id=", width="20" ,height="30" + gr.getValue('sys_id') + ">");

 

Kyle J
Tera Contributor

I hope you have figured this out by now.  I tired this and it worked.  The "//682017b893300300ac0c5999357ffb2d.iix" refers to the URL for an image in the image library.  I added a comma and width=xx, height=120 

 

 

template.print("<img src=/682017b893300300ac0c5999357ffb2d.iix, width=100, height=120>");
andreasicf
Tera Contributor

Thanks @Kyle J 

My apologies @Brian186 -- I didn't see your reply originally, but the notification for Kyle's helped bring my attention here. What Kyle proposed is great and would have been what I would have suggested.

 

You should have access to all sorts of neat img parameters you can specify as well, more examples can be found here: https://www.w3schools.com/tags/tag_img.asp 

Brian186
Tera Expert

Hi Kyle and Allen,

I'm really sorry for the late reply because I was on medical leave. I just got back to work yesterday. I still need your help with the attachment. I'm trying to get the image to show up in Outlook email, but it did not work. I must have done something incorrectly.

 

Here are the steps I have done:

1.  Created Email Script:

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {
 
    var gr = new GlideRecord('sys_attachment');
    gr.addQuery('table_sys_id', current.sys_id);
    gr.addQuery('content_type', "CONTAINS", "image"); //This line helps ensure you're only grabbing image type attachments
    gr.query();
    while (gr.next()) {
        //template.print("<img width='500' height='200' src=/sys_attachment.do?sys_id=" + gr.getValue('sys_id') + ">");
template.print("<img src=/682017b893300300ac0c5999357ffb2d.iix, width=100, height=120>");
        template.print("<br>");
        template.print("<br>");
    }
})(current, template, email, email_action, event);
 
2.  Attached the email script notification on sysapproval_approver table

Brian186_0-1702580537512.png

 

Allen Andreas
Administrator
Administrator

Hi @Brian186 

For my reply, I'd like to stay in the realm of what I posted about, meaning the code I provided. You shouldn't need to alter it too much to simple get the image width and height implemented correctly. I see you're using another users reply as your code, instead of what I offered.

 

Please review my code, but you can simple add height and width to it like so:

 

var gr = new GlideRecord('sys_attachment');
    gr.addQuery('table_sys_id', current.sys_id);
    gr.addQuery('content_type', "CONTAINS", "image"); //This line helps ensure you're only grabbing image type attachments
    gr.query();
    while (gr.next()) {
        template.print('<img src=/sys_attachment.do?sys_id=' + gr.getValue("sys_id") + ' width="300" height="300">');
    }

 

 

Brian186
Tera Expert

Hi Allen,

Thank you so much for continuing to help. After replacing the existing code with yours, I'm still struggling to get the image to show in the email.  Could you please take a look at below what I have done? Hopefully, you can spot where I made a mistake and provide suggestion on fixing it.

 

Brian186_0-1702586466103.png

 

Notification is on Approval Table

Brian186_1-1702586574413.pngBrian186_2-1702586620933.pngBrian186_3-1702586759030.png

 

Allen Andreas
Administrator
Administrator

Hi @Brian186 

I believe originally, when you first replied, you said the attachment was showing and everything was working fine it was just that the image appeared small?

Now, you're saying the image isn't showing at all?

 

Please understand that this code to show the image is assuming that the image is attached to the record upon which this notification is triggered. So, is there an "image" attachment on this "sysapproval_approver" record? If not, and the image is attached on another record, such as the record that this approval is for...then you'd need to adjust the code to not use the "current.sys_id" and instead, use the sys_id of the record that this approval is for. Which might instead need to be: "current.sysapproval".

Kyle J
Tera Contributor
I don't know what fancy stuff you are trying to do, but here is my email script I used.  So I uploaded the image I wanted to use in advance to the image library.  Then opened the image in a new page.  it opened a URL that displayed the image in the browser.  I took that URL pasted /c5d2435fdbb635105aaa4929139619e6.iix into the code.  Don't forget your commas.  I found as long as you include the comma you can use any html image tag in the code.  The first image below is the mail script for the image.  the second is workflow notification I used it in.  These work similar to email notifications since they are basically same in workflow form.  
 
 
(function runMailScript(current, template, email, email_action, event) {

    template.print('<br/><br/>');
 
    template.print("<img src=/c5d2435fdbb635105aaa4929139619e6.iix, title=Maze001, width=400, height=650>");

 
})(current, template, email, email_action, event);
 
KyleJ_0-1702589251818.png

 

KyleJ_1-1702589652558.png

 

Brian186
Tera Expert

Hello @Allen Andreas 

I'm really sorry for the confusion. When I first replied, I was attaching the email script to the sc_req_item record, and it was working fine, except I needed help with sizing the image bigger. Now, management wants to see all images attached to the approval record, so I created a notification on the sysapproval_approver table and used the same email script, thinking that it should work the same as in the sc_req_item record.

 

You pointed out that an image needs to be attached to the record upon which this notification is triggered. It is making complete sense now. I implemented your suggested code by replacing "current.sys_id" with "current.sysaproval", and it is working perfectly.

 

I sincerely thank you for your time and effort.

Allen Andreas
Administrator
Administrator

It's all good @Brian186 😀

I'm glad it all worked out in the end.

 

Version history
Last update:
‎03-04-2022 02:54 PM
Updated by: