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

Ankur Bawiskar
Tera Patron
Tera Patron

@AndreasRitter 

Sorry didn't get what is required as part of your requirement

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 

If a contact sends an e-mail without a reference ID in the body of the e-mail, but with the case number in the subject, then the e-mail should be assigned to the case.

@AndreasRitter 

are you saying the inbound email record should get updated with the Case record identified?

try updating and add this line

sys_email.target_table = "sn_customerservice_case";
sys_email.instance = caseGR.getUniqueValue();
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

@Ankur Bawiskar 

 

perhaps I did not express myself clearly 😞

 

I want to attach an email to a case via an email inbound action.
The case number is in the subject of the email.

 

The email should also be displayed in the activity stream of the case.