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

@tsoct 

seems your BR is running multiple times

Why not handle this in the inbound email action script itself and add your complete email as attachment to target record?

How to attach an inbound email as an attachment to an record from another table , if the inbound act... 

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,

 

I have the inbound action has a line in for GlideSysAttachment.copy, adding the 

GlideSysAttachment.write doesnt seems working.
 
The inbound action has target table = sc_request and the attachments should always be added to RITM table

 

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

    var cartId = GlideGuid.generate(null);
    var cart = new Cart(cartId);
    var subject = email.subject;

    var item;
    item = cart.addItem('3ae423dkfy89s7akla1243');
    cart.setVariable(item, 'description', email.body_text);
    var rc = cart.placeOrder();
	
    current.requested_for = gs.getUserID();

    var gr = new GlideRecord("sc_req_item");
    if (gr.get("request", rc.sys_id)) {
        gr.setValue('contact_type', 'email');
        gr.update();
        sys_email.target_table = "sc_req_item";
        sys_email.instance = gr.sys_id;
        GlideSysAttachment.copy('sys_email', sys_email.sys_id, 'sc_req_item', gr.sys_id);

        var fileName = email.subject + '.eml';
        var contentType = 'application/octet-stream';
        var content = email.content_type + "\n" + email.headers + "\n\n\n\n" + email.body_text;
        GlideSysAttachment.write(gr.sys_id, fileName, contentType, content);

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

 

 

@tsoct 

update line as this -> 1st parameter should be GlideRecord object and not record sysId

        GlideSysAttachment.write(gr, fileName, contentType, content);

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,

updated the line as suggest. i even tried added another line after 

gr.insert();
 
Still the email did not get attach to attachment. I cant find it from sys_attachment table as well.

@tsoct 

are you sure there is RITM associated to that REQ when your script runs?

what debugging have you done so far by adding logs?

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