Image from record producer not showing in email notification

maryamalona
Tera Contributor

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?

1 ACCEPTED SOLUTION

pr8172510
Kilo Sage

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);

pr8172510_0-1791889459609.pngpr8172510_1-1791889495297.png

 

View solution in original post

3 REPLIES 3

Tanushree Maiti
Tera Patron

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}

 

 

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

the variable is of type "image" in the table, and in the record producer it is of type "attachement".

pr8172510
Kilo Sage

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);

pr8172510_0-1791889459609.pngpr8172510_1-1791889495297.png