stop duplicate emails from inbound action

JagjeetSingh
Kilo Sage
Kilo Sage

hi,

i have an inbound action to create incidents from email.

The requirement is that i want to ignore incident creation if subject is same and add that mail content in existing incident itself.

Please let me know if any idea on this.

Jagjeet Singh
ServiceNow Community Rising Star 2022/2023
1 ACCEPTED SOLUTION

asifnoor
Kilo Patron

Hi jagjeet,

Assuming subject is stored in short description of incident.

in your inbound action, check like this. This way, if short description exists already for any incident, then it will update the same incident. otherwise, it will create any new incident.

var gr = new GlideRecord("incident");
gr.addQuery("short_description",email.subject.toString());
gr.query();
if(gr.next()) {
gr.work_notes=email.body_text;
gr.update();
}else {
current.short_description = email.subject;
current.description=email.body_text;
//add other fields here.
}

Kindly mark the comment as a correct answer and helpful if it helps to solve your problem.

Regards,
Asif
2020 ServiceNow Community MVP

View solution in original post

12 REPLIES 12

This worked. Many thanks!

i just had to add current.insert() function in else condition.

Jagjeet Singh
ServiceNow Community Rising Star 2022/2023

Dear Community,

 

I have similar requirement to update the existing incident if existing incident number is mentioned in subject of email. in my script existing incident is updating , however along with existing incident update, it is creating an incident too. Can someone help to just update the existing incident not to create new incident

Jaspal Singh
Mega Patron
Mega Patron

Hi Jagjeet,

 

You can try using below filter in your Inbound action

 

Thanks,

Jaspal Singh

Alok Das
Tera Guru

Hi Jagjeet,

I see that you already got the correct answer for your question however I would like to add few things which came up in my mind please find below the same:

1. What if the email is received with same short description and description but with different user?

2. What if the email is received with the same user but the previous incident is old and inactive?

Unfortunately for the above two scenarios the script which asifnoor provided will fail. So I added few more validations which will overcome the above two scenarios please find the updated script below:

(function runAction(/*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {
	
	var gr = new GlideRecord("incident");
	gr.addActiveQuery();                                      //search only for the active incidents
	gr.addQuery("short_description",email.subject.toString());//search for same short description
	gr.addQuery("description",email.body_text.toString());    //search for the same description
	gr.addQuery("caller_id",gs.getUserID());                  //search for the incident of same caller
	gr.query();
	if(gr.next()) {
		gr.work_notes=email.body_text;
		gr.update();
	}
	else {
		current.short_description = email.subject;
		current.description=email.body_text;
		current.caller_id=gs.getUserID();
		current.contact_type = "email";
		//add other fields here.
		current.insert();
	}
	
})(current, event, email, logger, classifier);

Regards,

Alok

very valid points Alok. Thanks for sharing. I've modified my script.

Jagjeet Singh
ServiceNow Community Rising Star 2022/2023