# Stop ticket re-opening after WP Closed

**URL:** <https://forum.sambapos.com/t/stop-ticket-re-opening-after-wp-closed/22063>\
**Category:** V5 Question\
**Created:** [April 26, 2020, 8:58pm UTC](https://forum.sambapos.com/t/stop-ticket-re-opening-after-wp-closed/22063 "2020-04-26T20:58:56Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![Memo](https://forum.sambapos.com/user_avatar/forum.sambapos.com/memo/32/55443_2.png) [@Memo](https://forum.sambapos.com/u/Memo)\
**Post date:** [April 27, 2020, 1:38am UTC](https://forum.sambapos.com/t/stop-ticket-re-opening-after-wp-closed/22063/3 "2020-04-27T01:38:49Z")

</div>

**I’ve edited this post to work with current work periods**

I’ll be reusing actions/rules from the following tutorial:

> [@Re-open Settled Ticket & Cancel Payments](https://forum.sambapos.com/t/re-open-settled-ticket-cancel-payments/1600):
>
> The short version of this originally posted here: Enable More Ticket Actions Module from Samba Market. This module enables Reopen Closed Ticket and Cancel Payments action types. Create two actions for both of these action types. These actions do not have parameters. Create “Display Ticket” action, name it Refresh Ticket. Set TicketId parameter as 0. Create an Automation Command, name it as you wish an place it somewhere suitable. Create a Rule to handle this command and call Actions Reo…

With the above tutorial, I did not see anywhere where the ticket number or ID is passed to any actions/rules.

The way around it is to manually enter the ticket number (visible on the top left of the ticket). There may be another way, but I know this one works.

Script:

Script Name: Reopen Check  
Handler: reopen

```
function check(ticketid)
{
  qtd = "SELECT FORMAT(Date, 'yyyy-MM-ddTHH:mm:ss') FROM Tickets WHERE Id = '"+ticketid+"'";
  var rtd = sql.Exec(qtd)[0];
  
  qwp = "SELECT FORMAT(StartDate, 'yyyy-MM-ddTHH:mm:ss') FROM WorkPeriods WHERE Id = (SELECT MAX(Id) FROM WorkPeriods WHERE StartDate = EndDate)";
  var rwp = sql.Exec(qwp)[0];

  if (rtd >= rwp)
    {
      return 1;
    }
  else
    {
      return 0;
    }
}

```

Actions:

![26-04-2020_19;08_1587949710_Samba.Presentation](https://forum.sambapos.com/uploads/default/original/3X/f/f/ff5309eca03c2407b9bd26bae8ed867a7c758a17.png)  
Action Name: Reopen - Load Ticket  
Action Type: Load Ticket  
Ticket Number: [:Ticket Number]

![26-04-2020_19;08_1587949727_Samba.Presentation](https://forum.sambapos.com/uploads/default/original/3X/6/d/6d8a351368618a06d75b841fc6feff1abe2989bb.png)  
(if you don’t already have one)  
Action Name: Execute Automation Command  
Action Type: Execute Automation Command  
Automation Command Name: [:Command Name]  
Command Value: [:Command Value]

![26-04-2020_19;32_1587951167_Samba.Presentation](https://forum.sambapos.com/uploads/default/original/3X/a/9/a988c9ef37770102a6f763104cda32d6448745b1.png)  
Action Name: MSG TEST  
Action Type: Show Message  
Message: [:Message]

Rules:  
 ![26-04-2020_19;34_1587951291_Samba.Presentation](https://forum.sambapos.com/uploads/default/original/3X/3/7/37e300614e526d5b113ca73d627d8baabd408c14.png)  
 ![26-04-2020_21;56_1587959798_Samba.Presentation](https://forum.sambapos.com/uploads/default/original/3X/0/f/0ffda9ca292db7ea819763947f444122142c7d3d.png)  
Rule Name: Reopen  
Event Name: Automation Command Executed  
Execute Rule if: [Matches]  
Custom Constraint:  
\*\*Automation Command [Equals] Reopen  
\*\* or set this to the button you currently have visible on the ticket screen

Actions to Add:

Action: Reopen - Load Ticket  
Constraint:  
Ticket Number: [?Ticket Number;\d\*;;OCN]

Action: Execute Automation Command  
Constraint: {CALL:reopen.check(‘{TICKET ID}’)} == 1  
Command Name: Reopen - Reopen Ticket  
Command Value: {TICKET ID}

Action: MSG TEST  
Constraint: {CALL:reopen.check(‘{TICKET ID}’)} == 0  
Message: Ticket out-of-range\rUnable to reopen

![26-04-2020_19;09_1587949770_Samba.Presentation](https://forum.sambapos.com/uploads/default/original/3X/3/a/3a783e4aa16634b9758fed537fc6a21535af4db1.png)  
Change the Custom Constraint for RU Reopen Settled Ticket:

Custom Constraint:  
Automation Command Name [Equals] Reopen - Reopen Ticket

Reopening ticket from today:  
 ![26-04-2020_19;30_1587951018_%pn](https://forum.sambapos.com/uploads/default/original/3X/4/3/4366a0048c1326bcd359c8a375ca0b752792ac6e.gif)

Reopening ticket from not today:  
 ![26-04-2020_19;43_1587951818_%pn](https://forum.sambapos.com/uploads/default/original/3X/e/9/e926db71f31fe755afde7b1d30c05442f9e6f7b9.gif)

EDIT: Updated script

---

_[View the full topic](https://forum.sambapos.com/t/stop-ticket-re-opening-after-wp-closed/22063)._
