inbound action to reopen ticket

jean-pauldehaas
Tera Guru

Hi,

 

We have a scoped application and need to create an inbound action which reopens the ticket.

when the ticket state is closed complete and the user responds on the last e-mail that was recieved for this ticket.

 

im reusing the Update incident BP inbound action which does this already for incidents but im not sure how to reconfigure it.

 

i already changed the tablename in line 3 but not sure what else needs to change:

 

gs.include('validators');

if (current.getTableName() == "x_lkqrp_eu_finance_eu_finance") {
   
    var gr = current;
   
    if (email.subject.toLowerCase().indexOf("please reopen") >= 0)
        gr = new Incident().reopen(gr, email) || gr;
   
    if (current.state != 7) {
        gr.comments = "reply from: " + email.origemail + "\n\n" + email.body_text;
    }
   
    if (gs.hasRole("itil")) {
        if (email.body.assign != undefined)
            gr.assigned_to = email.body.assign;
       
        if (email.body.priority != undefined && isNumeric(email.body.priority))
            gr.priority = email.body.priority;
    }
   
    var grusr = new GlideRecord('sys_user');
   
    if (current.state == 7) {
        grusr.addQuery('email', email.origemail);
        grusr.query();
        if (grusr.next()) {
            gs.log('JP test');
            gs.eventQueue("ticket.closed.autoreply", current, email.origemail, grusr.sys_id);
        }
    }
   
    if (current.state == 6) {
        grusr.addQuery('email', email.origemail);
        grusr.query();
        if (grusr.next()) {
            gs.log('JP test');
            gs.eventQueue("ticket.resolved.autoreply", current, email.origemail, grusr.sys_id);
        }
    }
   
    gr.update();
}
1 ACCEPTED SOLUTION

Runjay Patel
Giga Sage

Hi @jean-pauldehaas ,

 

You have to create new Inbound action for your table and use below script in.

gs.include('validators');
if (current.getTableName() == "x_lkqrp_eu_finance_eu_finance") {
   current.comments = "reply from: " + email.origemail + "\n\n" + email.body_text;
         current.state= '1'; // replace with actual field name and value of state for your table.
      current.update();
}

 

RunjayPatel_0-1734440236234.png

 

RunjayPatel_1-1734440298895.png

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

 

View solution in original post

3 REPLIES 3

Runjay Patel
Giga Sage

Hi @jean-pauldehaas ,

 

You have to create new Inbound action for your table and use below script in.

gs.include('validators');
if (current.getTableName() == "x_lkqrp_eu_finance_eu_finance") {
   current.comments = "reply from: " + email.origemail + "\n\n" + email.body_text;
         current.state= '1'; // replace with actual field name and value of state for your table.
      current.update();
}

 

RunjayPatel_0-1734440236234.png

 

RunjayPatel_1-1734440298895.png

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

 

@Runjay Patel thanks that seems to be working!

Uncle Rob
Kilo Patron

At about line 8 of your code you'll see
gr = new Incident().reopen(gr, email) || gr;

That's where I suspect the failure is happening because its instantiating a script include (Incident()) then running a function from it.  Probably getting some scoping issues.  ServiceNow is deliberately picky about what a scope can do with another scope's resources (even if that scope is global).  Plus that script include might have some assumptions about Incident being the target table.

So you're going to need to do a bit of dumpster diving.  Can be intimidating but a great learning opportunity.
In the Update incident BP inbound action on line 8 you'll find this code.  Did you know you can right click and view the definition of the script include?

UncleRob_0-1734440590707.png

 

Turns out when we land there it calls ANOTHER script include...

UncleRob_1-1734440637014.png

So when you finally land on IncidentSNC, you can find a function called reOpen.  And there you'll realize how much this all depends on INCIDENT being the table in question.  And you'll see there's even more script includes being called.

UncleRob_2-1734440748421.png


SO WHAT TO DO?

There are two main options

Option 1:  Simple but Messy

Take the basic parts of that re-open function and re-purpose them for your custom table, and drop that code right into your Inbound Action.  Why is that messy?  Because your functions should really be in a script include.  
Imagine if you had 4 different ways to re-open:  inbound action, scheduled job, UI policy, and a business rule.
Now lets say you want to improve that code some day.  Would you rather modify it in 4 places or 1?
If you said 1, then you need....

Option 2:  Harder but Better Practice

For this you'll need to understand script includes.  Make a new script include for your custom app and describe a function in there that does something similar to the IncidentSNC reopen function.  Then CALL that function from your inbound action.

If it were me, I'd go with Option 2