Email Script - How to check if attachment has attached file

Lisa Goldman
Kilo Sage

Hello,

 

I have the following two attachment variables in the Catalog Item.  

 

LisaGoldman_0-1710198483578.png

How to write an if else statement in email script to check if the attachment variable has attached file or not?

if attachment is there, displays "file is attached" else "please attach a file"

 

Thank you

 

 

 
1 ACCEPTED SOLUTION

Hi @Lisa Goldman ,

 

try with below code

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
     /* Optional EmailOutbound */
     email, /* Optional GlideRecord */ email_action,
     /* Optional GlideRecord */
     event) {
     var image1 = current.variables.attach_image1;
     var image2 = current.variables.attach_image2;

     if (image1 != '' && image2 != '') { // when both the variables have attachment attached
         var gr = new GlideRecord('sys_attachment');
         gr.addQuery('table_sys_id', current.sysapproval);
         gr.addQuery('content_type', "CONTAINS", "image");
         gr.query();
         while (gr.next()) {
             template.print('<img src=/sys_attachment.do?sys_id=' + gr.getValue("sys_id") + ' width="400" height="300">');
             template.print("<br>");
             template.print("<br>");
         }
     } else if(image1 != '' || image2 != '') { // when only 1 of the variable has attachment attached
var gr = new GlideRecord('sys_attachment');
         gr.addQuery('table_sys_id', current.sysapproval);
         gr.addQuery('content_type', "CONTAINS", "image");
         gr.query();
         if (gr.next()) {
             template.print('<img src=/sys_attachment.do?sys_id=' + gr.getValue("sys_id") + ' width="400" height="300">');
             template.print("<br>");
             template.print("<br>");
         }
}else{
         return false;
     }
 })(current, template, email, email_action, event);
 

 

Thanks,

Danish

View solution in original post

14 REPLIES 14

Thank you so much for assistance @Amit Verma 

There is no way I can get the code to work without the community and your help.  This is fantastic work. However, I run into a an issue when I attached image2 ONLY.  The header displays "Action" instead of "Impact". Could you please continue to help?  Thank you again.

LisaGoldman_0-1710304716868.png

FYI...  

Attached image1 only works fine

LisaGoldman_3-1710305029533.png

 

 

Attached image1 and 2 works fine

LisaGoldman_1-1710304958365.png

 

 

@Lisa Goldman 

 

Sorry. I messed up with the conditions. Please try below code :

 

(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template,
    /* Optional EmailOutbound */
    email, /* Optional GlideRecord */ email_action,
    /* Optional GlideRecord */
    event) {
    var image1 = current.variables.attach_image1;
    var image2 = current.variables.attach_image2;

    if (image1 != '' && image2 != '') { // when both the variables have attachment attached
        var bothAttachmentGR = new GlideRecord('sys_attachment');
        bothAttachmentGR.addQuery('table_sys_id', current.sysapproval);
        bothAttachmentGR.addQuery('content_type', "CONTAINS", "image");
        bothAttachmentGR.query();
        var flag = 0;
        while (bothAttachmentGR.next()) {
            if (flag == 0) {
                template.print('<h3>Action:</h3>');
            } else if (flag == 1) {
                template.print('<h3>Impact:</h3>');
            }
            template.print('<img src=/sys_attachment.do?sys_id=' + bothAttachmentGR.getValue("sys_id") + ' width="400" height="300">');
            template.print("<br>");
            template.print("<br>");
            flag = 1;
        }
    } else if (image1 != '' && image2 == '') { // when Image 1 of the variable has attachment attached
        var attachment1GR = new GlideRecord('sys_attachment');
        attachment1GR.addQuery('table_sys_id', current.sysapproval);
        attachment1GR.addQuery('content_type', "CONTAINS", "image");
        attachment1GR.query();
        if (attachment1GR.next()) {
            template.print('<h3>Action:</h3>');
            template.print('<img src=/sys_attachment.do?sys_id=' + attachment1GR.getValue("sys_id") + ' width="400" height="300">');
            template.print("<br>");
            template.print("<br>");
        } else if (image1 == '' && image2 != '') { // when Image 2 of the variable has attachment attached
            var attachment2GR = new GlideRecord('sys_attachment');
            attachment2GR.addQuery('table_sys_id', current.sysapproval);
            attachment2GR.addQuery('content_type', "CONTAINS", "image");
            attachment2GR.query();
            if (attachment2GR.next()) {
                template.print('<h3>Impact:</h3>');
                template.print('<img src=/sys_attachment.do?sys_id=' + attachment2GR.getValue("sys_id") + ' width="400" height="300">');
                template.print("<br>");
                template.print("<br>");
            }
        }
    } else {
        return false;
    }
})(current, template, email, email_action, event);

Please mark this response as correct and helpful if it assisted you with your question.

Thank you, @Amit Verma 

I am still encountering the same issue where it printed the header incorrectly.

LisaGoldman_0-1710348390546.png

Here is the area where I think is causing the problem. It is triggering from both variables block

 

LisaGoldman_1-1710348902795.png

 

Printed:

LisaGoldman_2-1710348989571.png

I think this block of code supposed to trigger, but it did not.

 

LisaGoldman_3-1710349127497.png

 

Please refrain from putting any more effort into making it works, because I will ask the users to enter the header name on the image before attaching so it will capture the right header name.

 

Many thank to your time and effort.  

 

 

 

 

 

Hi @Lisa Goldman ,

 

Please try the below script,

 

var gr = new GlideRecord('sys_attachment');
    var q = gr.addQuery('sys_id', current.variables.attach_image1);
    q.addOrCondition('sys_id', current.variables.attach_image2)
    gr.query();
    if (gr.hasNext()) {    
        while (gr.next()) {
            var url = gr.getTableName() + ".do?sys_id=" + gr.getValue('sys_id');
            var attachLink = '<a href="' + url + '">' + gr.file_name + '</a>';
            template.print(attachLink + "<br />");
        }
    }
 
Regards,
Deborah Brown
 
Please mark this response as correct or helpful if it assisted you with your question.

Hi @Lisa Goldman

 

Can you please try with the script provided by @Danish Bhairag2 . 

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.