Inbound mail script to check for attachments

anushad
Tera Expert

Hi ,

Can someone please help me to fix the below inbound mail script code.

Below code is written to verify whether the inbound email has attachments or not.If there are attachments then we have to setValue on a task and trigger an event. If not we have to abort the whole setValue action and trigger an event which sends an email notification . But both if(attachLog.next()) and else code parts are executing all the time. Instead of either one of the events both events are being triggered.

if (email.subject.contains("accept")) {

/var email_log = new GlideRecord('sys_email');
email_log.addQuery('uid', email.uid);
email_log.query();
if (email_log.next()) {
 var attachLog = new GlideRecord('sys_attachment');
attachLog.addQuery('table_sys_id', email_log.sys_id);
 attachLog.addQuery('table', 'sys_email');
attachLog.query();
 if (attachLog.next()) {
current.setValue("u_response"," accepted");
gs.info("In the inbound email action before triggering event");
gs.eventQueue('letter_response',current);
 }

else{

gs.info("In the inbound email action before triggering event");

gs.eventQueue('letter_response_attachdocs',current);

}
}

}

1 ACCEPTED SOLUTION

anushad
Tera Expert

I figured out what's wrong with the code attachLog.addQuery('table', 'sys_email'); the field name table in the query statement is what giving me the trouble , as it should be attachLog.addQuery('table_name', 'sys_email');

 

View solution in original post

9 REPLIES 9

siva_
Giga Guru

check below thread if that may help 

https://community.servicenow.com/community?id=community_question&sys_id=ffd01ba9dbdcdbc01dcaf3231f96194f

thanks 

siva

 

Thanks for the reply that post is related to size of the attachment.

Harsh Vardhan
Giga Patron

adding one sample code which you can use to check if email has an attachment or not.

 

//Query the sys_email table so we can get the logged email's sys id, this is used later to look for attachments.
var email_log = new GlideRecord('sys_email');
email_log.addQuery('uid', email.uid); //Since we are calling this from an inbound email action, we need to lookup this incoming uid in the sys_email table.
email_log.query();

//Make sure we can find the email in the email log
if (email_log.next()) {

//Query our attachment table
var attachLog = new GlideRecord('sys_attachment');
attachLog.addQuery('table_sys_id', email_log.sys_id);
attachLog.addQuery('table', 'sys_email');
attachLog.query();

if (attachLog.next()) {
//Do Stuff
}

}

 

based on the above code you can set the value . give a try. let us know if you stuck somewhere.

I have same code and its giving me a problem by executing both if(attachLog.next()) and else code parts.

I have included my code in the question.