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

HI @Ankur Bawiskar ,

 

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

 

@suuriyas 

did you debug and see what came in logs?

if the IF condition is not getting satisfied it won't update

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)) {
        gs.info('inside update for work notes');
        current.work_notes = "reply from : " + mailaddress + "\n\n" + email.body_text;
    } else {
        gs.info('inside update for comments');
        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;
}

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

HI @Ankur Bawiskar ,

 

HI @Ankur Bawiskar ,

 

 

Let me check that.

But can you please let me know that mailaddress is not declare anywhere above is it fine?

 

@suuriyas 

it might break as that variable is not defined.

what do you expect that variable to hold?

 

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

HI @Ankur Bawiskar ,

 

Added the logs in script and tested it.

Email is processed and mail display in targeted record but comment/worknotes  did not updated in targeted record.

And when i checked the logs 

"Update HR Case : did not create or update sn_hr_core_case using current" this is what the information i get and this warning

com.glide.script.RhinoEcmaError: "mailaddress" is not defined.
   sysevent_in_email_action.7a5370019f22120047a2d126c42e7063.script : Line(40) column(0)
     37: if (current.getTableName() == "sn_hr_core_case" && current.getValue('state') != 3 && current.getValue('state') != 7) {
     38: 
     39: //gs.info('xxx1 '+ mailaddress);
==>  40:     if (current.getValue('u_sender_email') == mailaddress || current.opened_for.email == mailaddress || current.opened_by.email == mailaddress || current.subject_person.email == mailaddress) {
     41: 
     42:
     43:         //if rejection email, set state back to work in progress
 Stack trace:
at sysevent_in_email_action.7a5370019f22120047a2d126c42e7063.script:40

 

i also get multiple information "slow ACL sysid for the path record/sn_hr_core_case/read , time was: 13"

and these messages did not present in logs  gs.info('inside update for work notes'); or gs.info('inside update for comments');

i have access to read/write in the hr record but not sure why it is not working