Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Reprocess Email Attach missing

FlorianV
Tera Contributor

Hello, I have an inbound email that is being classified as junk. Then, I execute a business rule and reprocess this email. Unfortunately, the attachment disappears afterward. Is it possible to save the attachment and add it back?

This is what my script looks like:

(function executeRule(current, previous /*null when async*/) {
	
	var userGl = new GlideRecord("sys_user");
	userGl.addQuery("email", current.user.replace("@test123.com", "@useremail.com"));

	userGl.query();
	if(userGl.next()) {
		current.user_id = userGl.sys_id;
		current.user = userGl.sys_id;
		current.update();
		reprocessEmail(current, userGl);
	}
	
	function reprocessEmail(current, user) {

		var evt = new GlideRecord('sysevent');
        evt.initialize();
        evt.user_id = user.sys_id;
        evt.user_name = user.user_name;
        evt.process_on = gs.nowDateTime();
        evt.name = "email.read";
        evt.parm1 = current.sys_id;
        evt.insert();
        gs.addInfoMessage(gs.getMessage('Event created to reprocess email "{0}"', Packages.org.apache.commons.lang.StringEscapeUtils.escapeXml(current.subject)));

        if (current.type == 'received-ignored') {
			
            current.type = "received";
        }

    }

})(current, previous);

Afterward, an inbound action should be executed to create an incident.

Thank you very much

3 REPLIES 3

Ratnakar7
Mega Sage

Hi @FlorianV ,

 

To address the issue of missing attachments when reprocessing an email, you'll need to ensure that the attachments are preserved and re-associated with the email record after it has been reprocessed. The problem arises because when you reprocess the email, the attachment association might be lost.

Here’s a approach to preserve the attachments and re-associate them with the email record:

  1. Store Attachments Before Reprocessing: Store the attachments in a temporary array or list before reprocessing the email.
  2. Re-associate Attachments After Reprocessing: After the reprocessing, re-associate the attachments with the email record.

Here is an updated version of your script with these steps included:

(function executeRule(current, previous /*null when async*/) {
    var userGl = new GlideRecord("sys_user");
    userGl.addQuery("email", current.user.replace("@test123.com", "@useremail.com"));
    userGl.query();
    if (userGl.next()) {
        current.user_id = userGl.sys_id;
        current.user = userGl.sys_id;
        current.update();
        reprocessEmail(current, userGl);
    }
    
    function reprocessEmail(current, user) {
        // Store the attachments in a temporary array
        var attachments = [];
        var attachmentGR = new GlideRecord('sys_attachment');
        attachmentGR.addQuery('table_name', current.getTableName());
        attachmentGR.addQuery('table_sys_id', current.sys_id);
        attachmentGR.query();
        while (attachmentGR.next()) {
            attachments.push(attachmentGR.sys_id.toString());
        }
        
        // Reprocess the email
        var evt = new GlideRecord('sysevent');
        evt.initialize();
        evt.user_id = user.sys_id;
        evt.user_name = user.user_name;
        evt.process_on = gs.nowDateTime();
        evt.name = "email.read";
        evt.parm1 = current.sys_id;
        evt.insert();
        gs.addInfoMessage(gs.getMessage('Event created to reprocess email "{0}"', Packages.org.apache.commons.lang.StringEscapeUtils.escapeXml(current.subject)));
        
        if (current.type == 'received-ignored') {
            current.type = "received";
        }

        // Re-associate attachments with the email record
        attachments.forEach(function(attachmentID) {
            var newAttachment = new GlideRecord('sys_attachment');
            if (newAttachment.get(attachmentID)) {
                newAttachment.table_sys_id = current.sys_id;
                newAttachment.update();
            }
        });
    }
})(current, previous);

 

Thanks,

Ratnakar

Thanks for the quick response, but unfortunately, it doesn't work.

The BR is set to when: before insert: checked order: 1

I have disabled this BR and implemented an after BR:

(function executeRule(current, previous /*null when async*/) {
	
	var userGl = new GlideRecord("sys_user");
	userGl.addQuery("email", current.user.replace("@test123.com", "@usermail.com"));

	userGl.query();
	if(userGl.next()) {
		current.user_id = userGl.sys_id;
		current.user = userGl.sys_id;
		current.update();

	}

})(current, previous);


Now the sys_email looks good, but the attachment is now missing in the incident.

Thanks for the quick response, but unfortunately, it doesn't work.

The BR is set to
when: before
insert: checked
order: 1

I have disabled this BR and implemented an AFTER BR:

(function executeRule(current, previous /*null when async*/) {
	var userGl = new GlideRecord("sys_user");
	userGl.addQuery("email", current.user.replace("@test123.com", "@usermail.de"));
	userGl.query();
	if(userGl.next()) {
		current.user_id = userGl.sys_id;
		current.user = userGl.sys_id;
		current.update();
	}
	
})(current, previous);

 

Now the sys_email looks good, but the attachment is now missing in the incident.