Ticket Rule: Last business day of every month
Answers (1)
I'm assuming that you didn't see my comment on https://www.itninja.com/question/sql-statement-help yesterday (remember that you have to Follow questions to get notified of comments, etc.).
Here is the query that I came up with:
SELECT
COUNT(ID), DAYOFMONTH(CREATED)
FROM HD_TICKET
WHERE YEAR(CREATED) = YEAR(NOW())
AND MONTH(CREATED) - MONTH(NOW())
AND DATE(
CASE DAYOFWEEK(LAST_DAY(NOW()))
WHEN 1 THEN DATE_SUB(NOW(), INTERVAL 2 DAY)
WHEN 7 THEN DATE_SUB(NOW(), INTERVAL 1 DAY)
ELSE LAST_DAY(NOW())
END ) = DATE(NOW())
GROUP BY dayofmonth(CREATED)
The important part is this:
AND DATE(
CASE DAYOFWEEK(LAST_DAY(NOW()))
WHEN 1 THEN DATE_SUB(NOW(), INTERVAL 2 DAY)
WHEN 7 THEN DATE_SUB(NOW(), INTERVAL 1 DAY)
ELSE LAST_DAY(NOW())
END ) = DATE(NOW())
My logic is that
- If the last day of the month is a Sunday (1), then the last business day was two days ago.
- If the last day of the month is a Saturday (7) then the last business day was one day ago.
- Otherwise the last day of the month is a weekday.
- If today is the last day of the month, then the entire statement is true and the query returns results, otherwise the query does not return results.
When I ran my query yesterday I got results, when I ran it today I didn't, which I took as a good sign. I have the rule set to run daily but it should only generate an email on the last day of the month. Of course, I won't know that for certain until November 29th. :)
You should be able to add the important part that is posted second to the where clause of your current rule.
Comments:
-
Thanks a ton for the reply here. I think I have something off in my Select SQL statement. When I click Run Now to test the syntax, I get the following error:
"11/07/2019 10:31:13> Starting: 11/07/2019 10:31:13 11/07/2019 10:31:13> Executing Select Query... 11/07/2019 10:31:13> mysqli error: [1064: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'COUNT(ID), DAYOFMONTH(CREATED) FROM HD_TICKET WHERE YEAR(CREATED) = YEAR(NOW()) ' at line 3] in EXECUTE("select "kbox_contact" as EMAILCOLUMN COUNT(ID), DAYOFMONTH(CREATED) FROM HD_TICKET WHERE YEAR(CREATED) = YEAR(NOW()) AND MONTH(CREATED) - MONTH(NOW()) AND DATE( CASE DAYOFWEEK(LAST_DAY(NOW())) WHEN 1 THEN DATE_SUB(NOW(), INTERVAL 2 DAY) WHEN 7 THEN DATE_SUB(NOW(), INTERVAL 1 DAY) ELSE LAST_DAY(NOW()) END ) = DATE(NOW()) GROUP BY dayofmonth(CREATED)")
Here's my Select SQL:
select
"kbox_contact" as EMAILCOLUMN
COUNT(ID), DAYOFMONTH(CREATED)
FROM HD_TICKET
WHERE YEAR(CREATED) = YEAR(NOW())
AND MONTH(CREATED) - MONTH(NOW())
AND DATE(
CASE DAYOFWEEK(LAST_DAY(NOW()))
WHEN 1 THEN DATE_SUB(NOW(), INTERVAL 2 DAY)
WHEN 7 THEN DATE_SUB(NOW(), INTERVAL 1 DAY)
ELSE LAST_DAY(NOW())
END ) = DATE(NOW())
GROUP BY dayofmonth(CREATED) - brianpink 5 years ago-
I wonder if it'd be easier to uncheck "Email each recipient in query results" and remove "kbox_contact" as EMAILCOLUMN and simply put a check mark in "Email results" and manually type the address in there? - brianpink 5 years ago
-
If you use that option you lose the ability to format the output of the message. Depending on the output of the query, however, that might be a good thing. The Email Results option will give you more of a report layout, which in this case is a good thing. - chucksteel 5 years ago
-
You're missing a comma after EMAILCOLUMN. - chucksteel 5 years ago
-
Well shoot, this ticket rule didn't generate a ticket during the last business day of the month for November. Did yours? - brianpink 4 years ago
-
Nope. Although I did find a typo. This line:
AND MONTH(CREATED) - MONTH(NOW())
should be
AND MONTH(CREATED) = MONTH(NOW())
I'm not sure why that didn't generate an error, or if it would have stopped the rule from returning any results, but it's possible. - chucksteel 4 years ago-
Thank you! I'll see how this goes at the end of this month. - brianpink 4 years ago
-
Hi Chuck, Thanks for all your help thus far my friend. I hope this additional post of mine makes it to your alerts so you're able to swing back. The ticket for sure opened on the last business day of the month, but unfortunately it created about 9 copies or so of the ticket. Any ideas on why that might have happened? - brianpink 4 years ago
-
Hi chucksteel,
So the ticket rule worked! It fired off a ticket today. The only problem is that it generated 9 copies of the ticket. Any idea on why that may have happened?
Thanks! - brianpink 4 years ago-
I'm guessing you are using the "Email each recipient in query results" and there are nine rows returned in the query. Use the "Email results" option instead. - chucksteel 4 years ago
-
Thanks for the reply! I made the change and will see how it goes in a couple weeks. - brianpink 4 years ago
The KACE SMA uses a MySQL like DB, I'm sure something like
DAYOFWEEK( LAST_DAY( NOW( ) )
Should be possible... but the it has to be a good SQL statement to achieve that... You can always contact KACE Professional Services Team... but they charge for stuff like this.
Or maybe someone from our Forum. - Channeler 5 years ago