Search code examples
loopscoldfusioncfmail

Auto generate a email based on a date (ColdFusion)


I have this big database table that contains 12 medical ceritifcations, expiration dates, links to files and what companies they're from. I need to generate a report via email within 90,60,30 and 15 days of the the certification expiring date.

Here's what the datebase looks like:

certID,
profileID,
cprAdultExp,
cprAdultcompany,
cprAdultImage,
cprAdultOnFile,
cprInfantChildExp,
cprInfantChildcompany,
cprInfantChildImage,
cprInfantChildOnFile,
cprFPRExp,
cprFPRcompany,
cprFPRImage,
cprFPROnFile,
aedExp,
aedcompany,
aedImage,
aedOnFile,
firstAidExp,
firstAidcompany,
firstAidImage,
firstAidOnFile,
emtExp,
emtcompany,
emtImage,
emtOnFile,
waterSafetyInstructionExp,
waterSafetyInstructioncompany,
waterSafetyInstructionImage,
waterSafetyInstructionOnFile,
bloodPathogensExp,
bloodPathogenscompany,
bloodPathogensImage,
bloodPathogensOnFile,
oxygenAdminExp,
oxygenAdmincompany,
oxygenAdminImage,
oxygenAdminOnFile,
lifegaurdingExp,
lifegaurdingcompany,
lifegaurdingImage,
lifegaurdingOnFile,
wildernessResponderExp,
wildernessResponderCompany,
wildernessResponderImage,
wildernessResponderOnFile, 
notes

How do I write some sort of loop to check all the dates (anything with EXP is a date) then store which ones are expiring, and email all those details to a person?


Solution

  • Since you have to send the email through CF (I presume) then the way I'd approach this is to run a scheduled task once a day that checks which rows have a 15, 30, 60 and 90 day expiry anniversary. So the scheduled task would run a few queries and then send the emails.

    The first thing is to actually find the rows in question (all my SQL presumes MS SQL Server - other RDBMSs will have similar syntax):

    <cfquery name="qExpiring">
      select
      certID,  
      dateDiff(day, cprAdultExp, getDate()) as cprAdultExpDaysSince
      dateDiff(day, cprInfantChildExp, getDate()) as cprInfantChildExpDaysSince
      from yourTable
      where 
      dateDiff(day, cprAdultExp, getDate()) in (15, 30, 60, 90)
      or 
      dateDiff(day, cprInfantChildExp, getDate()) in (15, 30, 60, 90)
    </cfquery>
    

    This should give you a result set like so:

    certID|cprAdultExpDaysSince|cprInfantChildExpDaysSince|etc.
    ___________________________________________________________
    xxxxxx|30                  |5                         |etc.
    xxxxxx|16                  |60                        |etc.
    xxxxxx|2                   |90                        |etc.
    

    Any that have matches on 15, 30, 60, 90 you are interested in processing futher. You could use a query of queries to do this:

    <cfquery name="qAdultExpRenewal" dbtype="query">
      select * from qExpiring
      where cprAdultExpDaysSince in (15, 30, 60, 90)
    </cfquery>
    

    You cn then loop over these records and send the appropriate email based on the value of cprAdultExpDaysSince.

    You're missing a fair bit of info to give you a comprehensive answer but I hope that this will point you in the right direction.