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.

reply emails shall be copied to associated child cases and incidents.

kalakaramad_20
Tera Contributor

I have a requirement that when the client replies to some cases from the email method the reply email must be copied to associated incident.

kalakaramad_20_0-1707910326595.png

The same content email received must be copied to the incident also. Can anyone help ?
Thanks in advance.

21 REPLIES 21

Hi @kalakaramad_20 

Yes, that's for inbound action only- 

(function runMailScript(email, template, table, record) {

    var inc = parseIncidentNumber(email.body);
    var emailBody = email.body_text; // Assuming plain text email

    var incGR = new GlideRecord('incident');
    if (incGR.get('number', inc)) {
        incGR.comments = "Client reply:\n" + emailBody;
        incGR.update();
        sendEmailToIncident(incGR.sys_id, emailBody);
    } else {
        // Your existing inbound action logic here
        var case_state = current.state;

        if (current.state == 500) {
            var lines = email.body_text.split('\n');
            var firstword = "";

            if (lines.length > 0)
                firstword = lines[0].replace(/^\s+|\s+$/g,'');
           
            firstline = firstword.toLowerCase();
           
            if (firstline) {
                if(firstline.indexOf("reject") == 0)
                    current.state = 2; // Set the state to 10 (assuming this means "rejected")
                else if(firstline.indexOf("accept") == 0)
                    current.state = 6;  
            }
        }
        if (current.getTableName() == 'sn_customerservice_case') {
                current.comments = current.comments = "reply from: " + email.origemail + "\n\n" + email.body_text;
        }
        else if (current.u_on_hold_reason_1 == 1) {
            current.state = 2;
        }
        else if (current.state == 200) {
            current.state = 2;
        }
        else {
            current.sys_updated_on = "";
        }
        current.update(); 
    }

})(email, template, table, record);

function parseIncidentNumber(emailBody) {
    // Logic to parse incident number from email body
    // Return incident number
}

function sendEmailToIncident(incidentSysId, emailBody) {
    // Logic to send email copy to associated incident
}

Please mark my answer helpful and correct. 

Regards,

Amit





hey tried this but the reply comments are not copied to the incident using this in the inbound action.

Hi @kalakaramad_20 

 

Can you share the sample email subject line and body so that I can relevant adjustments in the script.

 

Regards,

Amit

kalakaramad_20_0-1708071541925.png

 

kalakaramad_20_1-1708071736140.png

 

Could you please check.