We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

If To or CC is caller then add email body in additional comment

Samiksha2
Mega Sage

Hi All,

 

I have a requirement to add a condition in the Reply Inbound action if To or CC is caller then add email body in additional comment. Here is my script:

(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
if (current.getTableName() == "incident") {
	if((email.to== current.caller_id) ||(email.copied== current.caller_id)){
    current.comments = "reply from: " + email.origemail + "\n\n" + email.body_text;
    current.update();
}
}
})(current, event, email, logger, classifier);

 

Please let me know what I'm doing wrong.

 

Thanks,

Sam

8 REPLIES 8

Sohail Khilji
Kilo Patron

Hi @Samiksha2 ,

 

Here is the fixed script try i hope it helps...

 

(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
    if (current.getTableName() == "incident") {
        var callerId = current.caller_id.toString();
        if ((email.to && email.to.contains(callerId)) || (email.cc && email.cc.contains(callerId))) {
            current.comments = "Reply from: " + email.origemail + "\n\n" + email.body_text;
            current.update();
        }
    }
})(current, event, email, logger, classifier);

☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Hi @Sohail Khilji ,

Its not working.

 

Thanks,

Sam

Sohail Khilji
Kilo Patron

Hi @Samiksha2 ,

 

Try to add logs and see whats printing and what is not printing.

 

 if (current.getTableName() == "incident") {
        var callerId = current.caller_id.toString(); 

        // Check if the email was sent to or copied to the caller
        if ((email.to.indexOf(callerId)) || (email.cc.indexOf(callerId))) {
            
            current.comments = "Reply from: " + email.origemail + "\n\n" + email.body_text;
            current.update();
        }
    }

☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Hi @Sohail Khilji ,

 

Condition is not working. Additional comment is adding everytime.

 

Thanks,

Sam