Email link to case with case number from the subject

AndreasRitter
Mega Guru

Hello everyone

 

We have a special case where a ticket system sends tickets to us by e-mail and receives a reply. If an e-mail is sent from the ticket system to ServiceNow, the Ref_ID cannot be included in the body.
We have created an e-mail inbound action that extracts the case number contained in the subject and then attaches the e-mail to the recognized ticket.

If we want to write a comment, this is generated, as is a work note. Only the e-mail is not attached to the case.
here our inbound action script:

 

(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
 var subject = email.subject || '';
 var match = subject.match(/CS\d{7}/); // Searches for e.g. "CS1234567"

    if (!match) return;

    var caseNumber = match[0];
 gs.info("msg inbound email CS in Subject Case Number: " + caseNumber);

    // Load the case using the number
 var caseGR = new GlideRecord('sn_customerservice_case');
 caseGR.addQuery('number', caseNumber);
 caseGR.query();

    if (!caseGR.next()) return; // Case not found

    // Load the sender as Customer Contact
 var contactGR = new GlideRecord('customer_contact');
 contactGR.addQuery('email', email.origemail);
 contactGR.query();

    var validContactFound = false;

    while (contactGR.next()) {
 if (contactGR.account == caseGR.account) {
 validContactFound = true;
 gs.info("msg inbound email CS in Subject Account: " + caseGR.account.name + "Contact: " + contactGR.name);
 //break;
 }
    }

    if (!validContactFound) return; // No valid contact found for the company of the case
 gs.info("msg inbound email CS in Subject: no return");

    sys_email.target_table = "sn_customerservice_case";
 sys_email.instance = caseGR.getUniqueValue();

	caseGR.needs_attention =true;
caseGR.update();

})(current, event, email, logger, classifier);

 

I have understood it so far that

sys_email.target_table = "sn_customerservice_case"; sys_email.instance = caseGR.getUniqueValue();

attaches the e-mail to the case and also appears in the related list of e-mails.

Thank you very much for your help.

 

Kind Regards

Andreas

11 REPLIES 11

@AndreasRitter 

for the email to be seen in activity stream of case system should be able to identify the inbound email is associated with Case sysId

Unless the Target record field on Email record is populated, system can't associated.

After adding that line did the Target field get updated with the Case script finds?

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

@Ankur Bawiskar 

when I look in the sys_email_list.do and check the email, I see the following entry in the email LOG:

inbound email CS in Subject : did not create or update sn_customerservice_case using current

The strange thing is that sometimes it works and the email is assigned, but usually not.

@AndreasRitter 

so are you saying this line works sometimes and updates the Target Record field on sys_email record?

sys_email.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

AndreasRitter
Mega Guru

Is there perhaps a better way than my email inbound action?

It is important that the case number is in the subject line of the email and that no other information from ServiceNow is included in the email.

I appreciate any information

Muhammad Salar
Giga Sage

Hi @AndreasRitter 
Try this, this will attach a text file with your email body to case record.
try and let me know

var body = email.body_text || '';

var caseGR = new GlideRecord('sn_customerservice_case');
 caseGR.addQuery('number', caseNumber);
 caseGR.query();
if (caseGR.next()) {
        var attachment = new GlideSysAttachment();
        var attachmentID = attachment.write(caseGR, 'inbound.txt', 'text/plain', body.toString());
    }