Need to add attachments to the case from email notification

cp10
Tera Contributor

Hi,

I have a requirement to update the state value and add the attachments to the case from email.

I have created Inbound email action and tested but the attachments are stored in sys_email table instead of Case table. i tried below code but its not working, please help me on this.

Inbound Email action: Reply

 

(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {

        // Implement email action here
        var Finance = '';
        var regex = /FC[0-9]+/gi;
        if (regex.test(email.subject)) {
            var matchInc = email.subject.match(regex);
            Finance = matchInc[0];
        }

        if (Finance != '') {
            var num = new GlideRecord('x_bcb_fcm_case');
            num.addQuery('number', Finance);
            num.query();
            if (num.next()) {
                if (num.state == '18') {
                    num.state = 10; // Example: Set to "In Progress"
                    num.update();
                    // Get attachments from the email
                    var attGR = new GlideSysAttachment().getAttachments('sys_email', email.sys_id);
                    while (attGR.next()) {
                        attGR.table_name = 'x_bcb_fcm_case';
                        attGR.table_sys_id = num.sys_id;
                        attGR.update();

                    }
                }
            }
        }

        })(current, event, email, logger, classifier);
5 REPLIES 5

SanjanaK
Tera Expert

Hello @cp10 ,

 

You can simply copy the attachments from sys_email table to your Case table using GlideSysAttachment.copy() function instead of writing your code for // Get attachments from the email

 

************************************************************************

Hope the answer helps!

Happy Learning!

cp10
Tera Contributor

Hi Sanjana, Thanks for the reply. I have tried below code but its not working. could you please let me know what is the issue on this code.

 

(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {

    // Implement email action here
    var Finance = '';
    var regex = /FC[0-9]+/gi;
    if (regex.test(email.subject)) {
        var matchInc = email.subject.match(regex);
        Finance = matchInc[0];
    }

    if (Finance != '') {
        var num = new GlideRecord('x_bcb_fcm_case');
        num.addQuery('number', Finance);
        num.query();
        if (num.next()) {
            if (num.state == '18') {
                num.state = 10; // Example: Set to "In Progress"
                num.update();

                var sa = new GlideSysAttachment();
                var attachments = sa.getAttachments('sys_email', email.sys_id);

                for (var i = 0; i < attachments.length; i++) {
                    var attachmentId = attachments[i];
                    sa.copy('sys_email', email.sys_id, 'x_bcb_fcm_case', num.sys_id);
                   
                }

            }
        }
    }

})(current, event, email, logger, classifier);

Hey @cp10 ,

 

You can refer below code (I have also referred it from UI action: Move Attachment To Target Record)

var grEmail = new GlideRecord('sys_email_attachment');
grEmail.addQuery('email', email.sys_id);
grEmail.query();
while (grEmail.next()) {
    var attachment = new GlideRecord('sys_attachment');
    if (attachment.get(grEmail.attachment)) {

        attachment.table_name = email.target_table; // your table name 
        attachment.table_sys_id = email.instance;
        attachment.update();
    }
}

@cp10 

try this

I assume it's going inside that IF and finding that record

(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {

    // Implement email action here
    var Finance = '';
    var regex = /FC[0-9]+/gi;
    if (regex.test(email.subject)) {
        var matchInc = email.subject.match(regex);
        Finance = matchInc[0];
    }

    if (Finance != '') {
        var num = new GlideRecord('x_bcb_fcm_case');
        num.addQuery('number', Finance);
        num.query();
        if (num.next()) {
            if (num.state == '18') {
                num.state = 10; // Example: Set to "In Progress"
                num.update();

                new GlideSysAttachment().copy('sys_email', sys_email.sys_id, 'x_bcb_fcm_case', num.sys_id);

            }
        }
    }

})(current, event, email, logger, classifier);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader