Inbound Action to deal with closed incidents

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2019 07:36 AM
Hi,
I wanted to run all normal inbound actions on emails but add something in so that, if an incident was closed, the ticket would be updated and the sender would be notified that the target ticket was closed.
I found a post somewhere and copied the script over and ran it. It's designed to replace the OOTB Update Incident inbound action and caters for all eventualities resulting from an reply email associated with an existing incident.
I ran into a few problems with the script and so decided to post my completed script here. I couldn't find the original post otherwise I would give that person full credit for this. All I've done really is to tweak it to fit my specific use case and I'm certain the the original would work in other circumstances.
The Inbound Action:
Name: Update Incident (BP)
Order: Depends. I have it set to continue processing other rules just in case so it probably makes no real difference where this comes in the pecking order.
When To Run: Type: Reply
(No other criteria are specified meaning that this will run on EVERY reply email)
Action: Script:
gs.include('validators'); //calls script include that validates the format of certain values.
if (current.getTableName() == "incident") {
//our incident state for resolved is 6 and closed is 7
//catches replies to closed or resolved incidents
if(current.incident_state == 6 || current.incident_state == 7){
//Catches legitimate reopen on a resolved incident
if (email.subject.toLowerCase().indexOf("please reopen") >= 0 && current.state == 6){
current.incident_state = 2;
current.work_notes = 'The caller did not feel that this issue was resolved.';
current.comments = "Email reply from: " + email.origemail + "\n\n" + email.body_text;
current.close_notes = '';//clears the close notes field on reopened incidents
}
//Catches a attempted re-open of a closed incident
else if (email.subject.toLowerCase().indexOf("please reopen") >= 0 && current.incident_state == 7){
gs.eventQueue("incident.closed.noreopen", current, current.state, previous.state);
current.comments('User attempted to re-open this closed incident. Notification sent to user that closed incidents may not be re-opened');
}
//Catches a closure email for a resolved incident and closes it
else if (email.subject.toLowerCase().indexOf("please close") >= 0) {
current.incident_state = 7;
current.active = false;
current.work_notes = "The caller agreed that this issue was resolved";
}
//Catches direct replies to closed or resolved incidents - which we don't allow
else{
gs.eventQueue("incident.closed.nodirectreply", current, current.state, previous.state);
current.comments = "User replied directly to a closure email on this closed or resolved incident. Notification sent to user that emails on closed or resolved incidents may not be directly replied to.";
current.update();
}
}
else{
current.comments = "Email reply from: " + email.origemail + "\n\n" + email.body_text;
current.update();
}
}
I haven't tested this all yet but the issue I ran into was that the second to last "current.update" was outside of the else parenthesis {}. I moved this up a line and then manufactured an email that would satisfy the if/else and it updated the target ticket and fired the event.
It also had differences in the way that current.comments was used. It originally written as current.comments('etc etc') but I noted that in other parts of the script it was done with an = and used " instead. I don't think this matters. The two methods should be interchangeable BUT, changing this caused the script to work!!
Keen eyed readers will see that there are still issues with some of the ifs and elses such as that there is no current. update and the comments update is marked as above but for the specific use case I needed it for, it works.
I will test the other use cases that are required and amend the script as required. If I find any further issues I will post them here.
Please note that you would need to change the incident_state numbers to match your closed and resolved state values as needed and you'd need to register an event with the name incident.closed.nodirectreply and set up a notification to go out when it is fired.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2019 07:41 AM
Apologies. The version I posted above contains an error which shows in the log when it runs. Line 27 where the event is queued contains 2 x parameters that are passed to the event. There are current.state and previous.state.
I don't know why but that throws up this error in the log:
org.mozilla.javascript.EcmaError: Cannot read property "state" from null
Caused by error in sysevent_in_email_action.498e10410a0a0b4b007c8c7f63531747.script at line 27
Removing these solves the issue but it shouldn't happen. It's identified "current" by this point and should be able to read the state value. In fact, adding a gs.log (current.state) results in the state value being entered into the log.
If I sort this I will post.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-18-2019 02:28 AM
This appears to work but only for subject line and the close notes aren't nulled.
I'll publish this and work on the tweaks for a later release.
Happy to take any suggestions.....
gs.include('validators');
if (current.getTableName() == "incident") {
//our incident state for resolved is 6 and closed is 7
//catches replies to closed or resolved incidents
if(current.incident_state == 6 || current.incident_state == 7){
//Catches legitimate reopen on a resolved incident
if (email.subject.toLowerCase().indexOf("please reopen") >= 0 && current.state == 6){
current.incident_state = 2;
current.work_notes = 'The caller did not feel that this issue was resolved.';
current.comments = "Email reply from: " + email.origemail + "\n\n" + email.body_text;
current.close_notes = '';
current.update();
}
//Catches a attempted re-open of a closed incident
else if (email.subject.toLowerCase().indexOf("please reopen") >= 0 && current.incident_state == 7){
gs.eventQueue("incident.closed.noreopen", current);
current.comments = "User attempted to re-open this closed incident. Notification sent to user that closed incidents may not be re-opened";
current.update();
}
//Catches a closure email for a resolved incident and closes it
else if (email.subject.toLowerCase().indexOf("please close") >= 0) {
current.incident_state = 7;
current.active = false;
current.work_notes = "The caller agreed that this issue was resolved";
current.update();
}
//Catches direct replies to closed or resolved incidents - which we don't allow
else{
gs.eventQueue("incident.closed.nodirectreply", current);
current.comments = "User replied directly to a closure email on this closed or resolved incident. Notification sent to user that emails on closed or resolved incidents may not be directly replied to.";
current.update();
}
}
else{
current.comments = "Email reply from: " + email.origemail + "\n\n" + email.body_text;
current.update();
}
}