BR on the sys_email table to create emails received as attachments to RITM.

tsoct
Tera Guru

I created this before-insert Business rule in the 'sys_email' table to add emails received as attachments to RITM. However, debug returns the error message 'org.mozilla.javascript.Evaluator'.Exception: Cannot find the method com.glide.ui.SysAttachment.write(java.lang.String,org.mozilla.javascript.ConsString,string,org.mozilla.javascript.ConsString).'

What might have gone wrong?

 

(function executeRule(current, previous /*null when async*/ ) {
    var sysId = current.instance;

    var grB = new GlideRecord("sc_req_item");
    grB.addQuery('sys_id', sysId);
    grB.query();
    if (grB.next()) {
        gs.info("Debug: " + sysId); //This return a correct sysId
        if (grB.cat_item == '3ae423dkfy89s7akla1243') {
            var attachment = new GlideSysAttachment();
            var fileName = current.subject + '.eml';
            var contentType = 'application/octet-stream';
            var content = current.content_type + "\n" + current.headers + "\n\n\n\n" + current.body_text;
            var agr = attachment.write(sysId, fileName, contentType, content);
            gs.info("Debug: The attachment sys_id is: " + agr);
            //grB.insert();
        }
    }

})(current, previous);
9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

@tsoct 

are you trying to copy file received from incoming email to target record?

what's your business requirement here?

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

Hello @Ankur Bawiskar ,

The requirement is to attach the email received to the record. Not only the attachment within the email received, but the entire email should be converted into an attachment and attached to the RITM

@tsoct

please try to use after insert BR

update as this and see

(function executeRule(current, previous /*null when async*/ ) {
    var sysId = current.instance;

    var grB = new GlideRecord("sc_req_item");
    grB.addQuery('sys_id', sysId);
    grB.query();
    if (grB.next()) {
        gs.info("Debug: " + sysId); //This return a correct sysId
        if (grB.cat_item == '3ae423dkfy89s7akla1243') {
            var attachment = new GlideSysAttachment();
            var fileName = current.subject + '.eml';
            var contentType = 'application/octet-stream';
            var content = current.content_type + "\n" + current.headers + "\n\n\n\n" + current.body_text;
            var agr = attachment.write(grB, fileName, contentType, content);

            //grB.insert();
        }
    }

})(current, previous);

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

Thanks @Ankur Bawiskar ,

After-insert BR is not working. I set it to After-Update BR, and I see that the email received as an attachment, but the attachments were created as duplicates, and the attachment content is empty..