add email attachment to existing record

pawan kalyan
Tera Contributor

I have an existing table in service now, there is an email id field.

 

I need to create an inbound action when the email receives to my instance, it should check the sender email and email id in my table. If both are same. It should update email attachments to my existing records where email id is same as sender email address. Can someone help me with the code to achieve this in ServiceNow inbound actions

has context menu

3 REPLIES 3

Gangadhar Ravi
Giga Sage
Giga Sage

you can do GlideRecord based on that email and then get sys_id of record you want to copy attachment to and then use below function to copy attachment. 

 

GlideSysAttachment.copy('sys_email',sys_email.sys_id,'table_name', gr.sys_id);

 Please mark my answer correct and helpful if this works for you.

Community Alums
Not applicable

Hi @pawan kalyan ,

Please try creating a Inbound action for the table you need the email to be processed.

You can try to use the below logic to get the requirement done-

 

(function executeRule(current, email, email_action, event) {
    var senderEmail = email.origemail.trim().toLowerCase();
    // Query the table to find a record matching the email address
    var gr = new GlideRecord('your_table_name'); // Replace 'your_table_name' ideally it should be sys_user
    gr.addQuery('email_id', senderEmail); // Replace 'email_id' with the field that holds the email in your table
    gr.query();
    if (gr.next()) {
        if (email.attachments) {
            GlideSysAttachment.copy('sys_email', email.sys_id, gr.getTableName(), gr.sys_id);
        }
    } else {
        gs.info("No record found for email: " + senderEmail);
    }
})(current, email, email_action, event);

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

 

we tried the same it is not working, when we tried sending mail to my ServiceNow instance , we are receiving the  mail but the target table is coming blank in email logs