- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2024 04:16 PM
Hello,
I have the following two attachment variables in the Catalog Item.
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2024 12:39 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2024 11:01 PM
Hello @Amit Verma
There are two attachment variables in the Catalog Item.
If I attach image 1 and leave image 2 unattached, it prints double images in the body of an email.
I need help on modifying the following email script to only print the variable that has the attachment.
Here is the code, please help. Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2024 12:39 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2024 01:23 PM
Hello @Danish Bhairag2
Thank you so much for helping and I apologize for the late response. I was troubleshooting an issue out the field.
The code you provided works well. However, could we make a few changes, add a header name for each attached images, because this is an IT Email Notification
If image1 is attached, added header "Action" else leave it blank
If image2 is attached, added header "Impact" else leave it blank
If both images are attached, image1 header "Action" and image 2 header "Impact"
Example:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2024 08:09 PM
Please try with the updated code below for the headers :
(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);
Thanks & Regards
Amit Verma
Please mark this response as correct and helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2024 09:29 PM
If my response is able to help you, please mark the answer as correct for others to reference.
Thanks and Regards
Amit Verma
Please mark this response as correct and helpful if it assisted you with your question.