How the email thread should happen to the customer and agent

mania
Tera Contributor

Hi,

 

I am facing some issue here.

I have a scenario here, when someone replies by email, the reply text + inline images + attachments are added to the correct case automatically. 

But here the issue is when i reply from the email the body is mapping to the worknotes.

mania_0-1755511757267.png

 

But i want the email thread should happen to the customer and agent 

Reply inbound:

(function runAction( /* GlideRecord */ email, /* GlideRecord */ event) {

    var subject = email.subject + "";
    gs.info("Email Subject: " + subject);

    // Regex to find case number pattern: CS + digits (e.g., CS12345)
    var caseNumberMatch = subject.match(/\bCS\d+\b/);
    var caseNumber = null;

    if (caseNumberMatch) {
        caseNumber = caseNumberMatch[0];
        gs.info("Extracted Case Number: " + caseNumber);
    } else {
        gs.info("No Case Number Found in Subject");
        return; // Exit early if no case number found
    }

    // Look up the related case
    var existingCase = new GlideRecord('sn_customerservice_case');
    if (existingCase.get('number', caseNumber)) {

        gs.info("The case exists: " + existingCase.number);
        gs.info("The Email sys_id is: " + sys_email.sys_id);

        // ---- Case updates ----

        // Adjust time_worked by +1 second
        var timeWorkedRaw = existingCase.getValue('time_worked'); // Example: "1970-01-01 00:02:02"
        var gdt = new GlideDateTime(timeWorkedRaw);

        var ms = gdt.getNumericValue();
        var totalSeconds = ms / 1000;
        totalSeconds += 1; // Add 1 second

        var gd = new GlideDuration(totalSeconds * 1000);
        existingCase.setValue('time_worked', gd.getDurationValue());

        // Append reply content with sender email & maintain inline images
        var senderEmail = email.origemail || ""; // gets replier's email
        gs.info("Reply sender email: " + senderEmail);

        // Append reply content (maintains inline image HTML)
        gs.info("The HTML field is " + email.body_html);
        existingCase.u_add_worknotes = "reply from: " + senderEmail + "<br><br>" + email.body_html;
        existingCase.update();

        // ---- Copy attachments from email to case ----
        copyAttachmentsFromEmailToCase(sys_email.sys_id, existingCase.sys_id);
    }

    /**
     * Copies all attachments from a sys_email record to a target case.
     * Skips duplicates based on file name + size.
     */
    function copyAttachmentsFromEmailToCase(emailSysId, caseSysId) {
        gs.info("Copying attachments from Email: " + emailSysId + " to Case: " + caseSysId);

        var gsa = new GlideSysAttachment();

        // Find attachments linked to this email
        var emailAttachmentGR = new GlideRecord('sys_attachment');
        emailAttachmentGR.addQuery('table_name', 'sys_email');
        emailAttachmentGR.addQuery('table_sys_id', emailSysId);
        emailAttachmentGR.query();

        while (emailAttachmentGR.next()) {

            var fileName = emailAttachmentGR.getValue('file_name');
            var sizeBytes = emailAttachmentGR.getValue('size_bytes');
            var sourceSysId = emailAttachmentGR.getValue('sys_id');
            var contentType = emailAttachmentGR.getValue('content_type');

            // Check for existing attachment on the case to avoid duplicates
            var caseAttachmentGR = new GlideRecord('sys_attachment');
            caseAttachmentGR.addQuery('table_name', 'sn_customerservice_case');
            caseAttachmentGR.addQuery('table_sys_id', caseSysId);
            caseAttachmentGR.addQuery('file_name', fileName);
            caseAttachmentGR.addQuery('size_bytes', sizeBytes);
            caseAttachmentGR.query();

            if (!caseAttachmentGR.hasNext()) {
                var caseGR = new GlideRecord('sn_customerservice_case');
                if (caseGR.get(caseSysId)) {
                    var stream = gsa.getContentStream(sourceSysId);
                    gsa.writeContentStream(caseGR, fileName, contentType, stream);
                    gs.info("Copied attachment: " + fileName);
                }
            } else {
                gs.info("Skipping duplicate attachment: " + fileName);
            }
        }
    }

})(email, event);

Can anyone please help on this, it will be useful.

Thanks!

 

5 REPLIES 5

Mark Manders
Mega Patron

I don't really understand what you want to achieve (what do you mean by "email thread should happen to the customer and agent"?)

The script you shared already told us that you are using some custom field (u_add_worknotes) that adds stuff to the work notes. If that shouldn't happen, update that part of the code.

Also: check out inbound email flows. It will take care of your script in an easier way (for instance: a simple 'copy attachments' function, only asks you to drag 2 records to it, no need for a huge script to do this).


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark