- 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-12-2024 09:43 PM
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.
FYI...
Attached image1 only works fine
Attached image1 and 2 works fine
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2024 10:15 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2024 10:00 AM
Thank you, @Amit Verma
I am still encountering the same issue where it printed the header incorrectly.
Here is the area where I think is causing the problem. It is triggering from both variables block
Printed:
I think this block of code supposed to trigger, but it did not.
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2024 12:43 AM
Hi @Lisa Goldman ,
Please try the below script,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-12-2024 12:51 AM
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.