The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Body of the Received email need to be update in comments/worknotes of the record

suuriyas
Tera Contributor

HI Community,

 

I have a requirement, in HR Case record whenever we receive an email reply from user then it needs to update in the comments/worknotes .

I have written a inbound action script for it but it is not working as expected.

email get added to the activity log of the record but the email body is not getting updated in the comments/worknotes.

 

For example: email received is updated but the body of the email is not getting updated in record's comments/worknotes.

what am i missing 

suuriyas_0-1756378986339.png

Part of the script:

//update on active cases
if (current.getTableName() == "sn_hr_core_case" && current.getValue('state') != 3 && current.getValue('state') != 7) {

        //gs.info('xxx1 '+ mailaddress);
    if (current.getValue('u_sender_email') == mailaddress || current.opened_for.email == mailaddress || current.opened_by.email == mailaddress || current.subject_person.email == mailaddress) {

       
        //if rejection email, set state back to work in progress
        if ((email.subject.indexOf(REJECT_SUBJ) !== -1) ||
            (email.subject.indexOf(REOPEN_SUBJ) !== -1)) {
               

            //only for awaiting acceptance state
            if (current.getValue('state') == 20) {
               
                current.state = 18;
            }
        }
        //If accept email close the case.
        if (email.subject.indexOf(ACCEPT_SUBJ) !== -1) {
            //only for awaiting acceptance state
            if (current.getValue('state') == 20) {
                current.state = 3;
            }
        }
    }
    if (current.subject_person.email != mailaddress && current.opened_for.email != mailaddress && current.opened_by.email != mailaddress && current.u_sender_email != mailaddress && !isInList(current.watch_list, mailaddress)) {
       
        current.work_notes = "reply from : " + mailaddress + "\n\n" + email.body_text;
    } else {
        current.comments = "reply from: " + mailaddress + "\n\n" + email.body_text;
    }
    current.update();

 

 

10 REPLIES 10

suuriyas
Tera Contributor

Just incase full script: having a doubt on mailaddress part as it is not declared

 

gs.include('validators');

//These should be defined as properties (notification sciprt HR Case Resolved)
var REJECT_SUBJ = "reject";
var REOPEN_SUBJ = "reopen";
var ACCEPT_SUBJ = "accept";

var senderSysID = email.from_sys_id.toString();
//string senders mail address from header info
// var SEARCH_STRING = "smtp.mailfrom=";
// var headers = email.headers;
// var smtp_mailfrom_pos = headers.indexOf(SEARCH_STRING);
// var email_start_pos = smtp_mailfrom_pos + SEARCH_STRING.length;
// var headers_tail = headers.substr(email_start_pos, headers.length);
// var email_end_pos = headers_tail.indexOf(' ');
// var mailaddress = headers_tail.substr(0, email_end_pos + 1).trim();
// mailaddress = mailaddress.replace(';', '');

if (sys_email.headers.indexOf("X-Original-Sender:dse@eumail.docusign.net")>-1) {
    var emails = "dse@eumail.docusign.net";
} else {
    var replyTo = sys_email.reply_to;
    var emails = email.from.toLowerCase();

    if (replyTo != '' && replyTo.indexOf("<") > -1) {
        var emStart = replyTo.indexOf("<") + 1;
        var emEnd = replyTo.indexOf(">");
        if (emStart >= 0) {
            emails = replyTo.substr(emStart, emEnd - emStart).trim();
        }
    } else if (replyTo != '') {
        emails = replyTo.toString();
    }
}

//update on active cases
if (current.getTableName() == "sn_hr_core_case" && current.getValue('state') != 3 && current.getValue('state') != 7) {

        //gs.info('xxx1 '+ mailaddress);
    if (current.getValue('u_sender_email') == mailaddress || current.opened_for.email == mailaddress || current.opened_by.email == mailaddress || current.subject_person.email == mailaddress) {

       
        //if rejection email, set state back to work in progress
        if ((email.subject.indexOf(REJECT_SUBJ) !== -1) ||
            (email.subject.indexOf(REOPEN_SUBJ) !== -1)) {
               

            //only for awaiting acceptance state
            if (current.getValue('state') == 20) {
               
                current.state = 18;
            }
        }
        //If accept email close the case.
        if (email.subject.indexOf(ACCEPT_SUBJ) !== -1) {
            //only for awaiting acceptance state
            if (current.getValue('state') == 20) {
                current.state = 3;
            }
        }
    }
    if (current.subject_person.email != mailaddress && current.opened_for.email != mailaddress && current.opened_by.email != mailaddress && current.u_sender_email != mailaddress && !isInList(current.watch_list, mailaddress)) {
       
        current.work_notes = "reply from : " + mailaddress + "\n\n" + email.body_text;
    } else {
        current.comments = "reply from: " + mailaddress + "\n\n" + email.body_text;
    }
    current.update();

} else if (current.state == 3 || current.state == 4) {
   

    if (email.subject.indexOf("Tikettisi on ratkaistu") > -1) {

    } else {
       
        var grHR = new GlideRecord('sn_hr_core_case');
        grHR.initialize();
        grHR.priority = current.priority;
        grHR.opened_for = current.opened_for;
        grHR.subject_person = current.subject_person;
        grHR.contact_type = 'email';
        grHR.topic_category = current.topic_category;
        grHR.topic_detail = current.topic_detail;
        grHR.hr_service = current.hr_service;
        grHR.state = 10;
        grHR.u_sender_email = current.u_sender_email;
        grHR.watch_list = current.watch_list;
        grHR.description = current.description;
        grHR.rich_description = current.rich_description;
        grHR.short_description = '[REOPENED ' + current.number + '] ' + current.short_description;
        grHR.parent = current.sys_id;
        grHR.comments = "Reply from: " + mailaddress + "\n\n" + email.body_text;
        grHR.insert();
    }
}

function isInList(userList, emailAdd) {
   
    var assgUser = new GlideRecord('sys_user');
    assgUser.addQuery('sys_id', 'IN', userList);
    assgUser.addQuery('email', emailAdd);
    assgUser.addQuery('active',true);
    assgUser.query();
    if (assgUser.next()) {
        return true;
    } else if (userList.toString().indexOf(emailAdd) > -1)
        return true;
    return false;
}

@suuriyas 

check my below response.

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

HI @Ankur Bawiskar ,

 

Yes mail is processed and user can able to write in the record

Ankur Bawiskar
Tera Patron
Tera Patron

@suuriyas 

is your inbound action getting processed or not?

Did you check the user who is responding has write access to work_notes and comments and also to the record as well?

If not then it won't update.

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