How to detect keywords from email and set category and subcat in inbound email action in Incident?

The Matrix
Tera Contributor

Hi All,

I am working on a POC and need some guidance.

Is it possible to detect specific keywords in the email body and automatically set the Category and Subcategory of an Incident through an inbound email action? What would be the best approach to achieve this?

4 REPLIES 4

pavani_paluri
Tera Guru

Hi @The Matrix ,

 

We can achieve this usning Inbound action. You can refere Solved: I want to set the category to the incident form fr... - ServiceNow Community

 

Also see below code for reference:

 

if (email.body_text.includes("database error")) {
current.category = "Applications";
current.subcategory = "Database";
}
else if (email.body_text.includes("network issue")) {
current.category = "Infrastructure";
current.subcategory = "Network";
}

 

Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Pavani P

Ankur Bawiskar
Tera Patron

@The Matrix 

yes why not

You can use RegEx or indexOf() to search that particular keyword and then set your category and subcategory

where are you stuck?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

vaishali231
Tera Guru

hey @The Matrix 

Approach

Read the email subject and body.

Convert the content to lowercase to make keyword detection case-insensitive.

Check for specific keywords.

Set category and subcategory before the record is inserted.

This works well for a POC and keeps the logic simple.

Sample Inbound Email Action Script

(function runAction(/*GlideRecord*/ current,
/*GlideRecord*/ event,
/*EmailWrapper*/ email,
/*ScopedEmailLogger*/ logger,
/*EmailClassifier*/ classifier) {

// Combine subject and body for better matching
var body = (email.body_text || "").toLowerCase();
var subject = (email.subject || "").toLowerCase();
var content = body + " " + subject;

// Keyword-based categorization
if (content.indexOf("vpn") > -1) {
current.category = "network";
current.subcategory = "vpn";
}
else if (content.indexOf("outlook") > -1 || content.indexOf("email issue") > -1) {
current.category = "software";
current.subcategory = "email";
}
else if (content.indexOf("laptop") > -1) {
current.category = "hardware";
current.subcategory = "laptop";
}

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

*************************************************************************************************************
If this response helps, please mark it as Accept as Solution and Helpful.
Doing so helps others in the community and encourages me to keep contributing.

Regards
Vaishali Singh

yashkamde
Tera Guru

Hello @The Matrix ,

 

Try out below code as shown in image. And always send the correct choice label in the mail.

This will help to detect keywords from email and set category and subcat in Incident..

 

yashkamde_0-1770621296324.png

(function runAction( /*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {

if (email.body.category != undefined) {
current.category = getChoiceValue(current.getTableName(), "category", email.body.category.trim());
}
if (email.body.sub_category != undefined) {
current.subcategory = getChoiceValue(current.getTableName(), "subcategory", email.body.sub_category.trim());
}

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

function getChoiceValue(table, field, label) {
var choiceGr = new GlideRecord("sys_choice");
choiceGr.addQuery("name=" + table);
choiceGr.addQuery("element=" + field);
choiceGr.addQuery("label=" + label);
choiceGr.query();

if (choiceGr.next()) {
return choiceGr.value.toString();
}

return label;
}

 

If my response helped mark as helpful and accept the solution.