Our maintenance department uses a CMMS package called MP2 for all maintenance work orders. The software is based on a MSSQL back end which has allowed us to create many additional reports and web interfaces to help the maintenance staff handle the work orders. Some of the work orders are scheduled PMs and they have a due date. Part of their review is making sure they have the work orders closed on time so to help them out I created a SQL Server Reporting Services report that will email them a list of work orders that are due the next business day.
First I have to get a listing of all the work orders that are due the next business day and then if it is Friday, get all the work orders that are due up through Monday. Below is the TSQL to get that info:
select
wo.wonum,
woeqlist.location,
equip.description,
wo.taskdesc,
wo.wotype,
wo.atfirstname + ' ' + wo.atlastname as fullname,
wo.schedfinishdate
from
wo left join woeqlist on wo.wonum = woeqlist.wonum
join equip on woeqlist.eqnum = equip.eqnum
where
schedfinishdate
between
cast(convert(char, getdate(), 101) as datetime) + 1 and
case
when (select datename(dw, getdate())) = 'Friday' then cast(convert(char, getdate(), 101)as datetime) + 3
else cast(convert(char, getdate(), 101) as datetime) + 1
end
and status != 'C'
order by wonum
This will return the work order number, which plant location the equipment’s name, what the title of the work order is, type of work order, who is assigned to the work order and the date it is due as a extreme reminder which day they need to complete this job.
Throw that in a Reporting Services report and we are almost ready. I don’t want to send an email unless there is actually a work order that is due for the following day. For this I create a Data Driven Subscription in the Reporting Services web page for this report.
To do this go to the report you want to send out in the Reporting Services web page and click on the Subscriptions tab. Select the New Data Driven Subscription. Give this subscription a description and select the Shared Data Sources option and click Next. Pick your shared data source and click Next. Now we will make a slightly modified query to get back an email address if any records are available.
select
email = 'MaintenanceDept@domain.com'
from
wo
where
schedfinishdate
between
cast(convert(char, getdate(), 101) as datetime) + 1 and
case
when (select datename(dw, getdate())) = 'Friday' then cast(convert(char, getdate(), 101)as datetime) + 3
else cast(convert(char, getdate(), 101) as datetime) + 1
end
and status != 'C'
group by email
This will return us just one record of MaintenanceDept@domain.com if there are any number of work orders due for the next business day.
On the next page in the TO: field select the database field email to include the needed email address. Set any other properties on this page and click Next. Set the days and hours that you want the report to run and you are done.
Now an email will go out only when there are work orders that are due tomorrow and if no work orders are do, no one gets an email.
In a later post, I will show a super cool user defined function that you can use to take into account holidays and other office closed events.