I want to set the category to the incident form from Email

Neethi2
Tera Expert

When I send the below data via email to ServiceNow,

Category: Apps - Data to Report

Sub Category: Master Data

 

It is not taking the Category and Sub category.

 

If I give the value of the choices in an email like below, it is setting the category and sub category.

Category: Apps - data

Sub Category: master

 

How to write a script to take the label from email and check the label in the choice list and then set it to incident category

1 ACCEPTED SOLUTION

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

 

(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;
}

 

jaheerhattiwale_0-1672815210452.png

 

 

Please mark as correct answer if this solves your issue.

 

 

View solution in original post

4 REPLIES 4

jaheerhattiwale
Mega Sage

@Neethi2 Have you created inbound action?

Yes

Here is the script,

if (email.body.category != undefined) {
current.category= email.body.category;
}
if (email.body.sub_category != undefined) {
current.subcategory= email.body.sub_category;

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

 

(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;
}

 

jaheerhattiwale_0-1672815210452.png

 

 

Please mark as correct answer if this solves your issue.

 

 

Thanks a lot.It worked