5 Replies Latest reply on Sep 4, 2012 10:25 AM by philmodjunk

    Time-Dependent Automated Email Script

    RobMad

      Title

      Time-Dependent Automated Email Script

      Post

      Hi all,

       

      I am new to FileMaker and was asked by my company to write a script that will automatically generate an email at midnight for those with appointments in ten days.  I've never written a script before and from what I can learn, it is not the easiest one to start with.  Any help would be appreciated!

      Here is what I have:

       

      Perform Script [CheckEmail]

      Set Error Capture [On]

      If (Admin::DateAppointment)-Get(CurrentDate)=10)

           If [Get(FoundCount) = 0]

                Exit Script []

           End if

           Set Error Capture [Off]

           Go to Record/Request/Page [First]

           If {$$MailerType = 2)

                Loop

                     Perform Script ["SMTPit_Initialize"]

                     If [Trim(Admin::EmailH1)<>"" and PatternCount (Admin::EmailH1: "@")]

                          Set Field [Globals::SMTPit AddressTo: Substitute (Admin::EmailH1 " ": "")]

                          Set Field [Globals::SMTPit Message;"Dear Admin::Envelope1, blahblahblah"]

                          Set Field [Globals::SMPTit Result; External("SMTP-SibjectAssign:; "Appointment Reminder")]

                          Set Field [Globals::SMPTit Result; External("SMTO-Send"; "Transcript="&Globals::SMTPit Transcript")]

                          If (PatternCount(Globals::SMTPit Reslt; "ERROR") = 0]

                          End if

                     End if

                     Go to Record/Request/Page [Next; Exit after last]

                End Loop

           End if

       

      Any help or thoughts would be MUCH appreciated.

       

      Thanks!

        • 1. Re: Time-Dependent Automated Email Script
          schamblee

          This script only checks the appointment date on the current record then you go to the first record  and perform your loop.  I would think you would need to check each clients appointment date therefore it would need to be inside the loop.  I'm not sure what mailer type 2 is but I would assume this is something that would also need to be checked with each client record, which would also mean it would need to be inside the loop.

           

          Or you could leave out of loop if you peform a find on records with 10 days before appointment date and mailer type2 which would limit your records to the found set.  Again Im not sure what type 2 mailer is but I assume that is clients that want to be emailed.

          • 2. Re: Time-Dependent Automated Email Script
            Sorbsbuster

            It's hard to advise completely as there are chunks of the script that you don't show, as S Chamblee pointed out.

            Should the first step be 'Perform Find'?  Either if it should, or the script called is a perform find script, then I would say the Set Error Capture should be before the find is performed.

            Your setting of Globals seems overly-complex.  All you have to do is use one script step - Send Mail, and within that step you can specify the SMTP Server settings, and call up by calculation (presumably by reference back to a related Contact Record) the e-mail address appropriate for the record you are on.

            • 3. Re: Time-Dependent Automated Email Script
              philmodjunk

              To point out another problem with the same section of your script:

              If (Admin::DateAppointment)-Get(CurrentDate)=10)

                   If [Get(FoundCount) = 0]

                        Exit Script []

                   End if

              If Get ( FoundCount ) is 0, then the first expression: (Admin::DateAppointment)-Get(CurrentDate)=10) cannot be true so your script will never exit due to an empty found set.

              Should be:

              Enter Find Mode []
              Set Field [admin::DateAppointment ; Get ( CurrentDate ) + 10 ]
              Set Error Capture [on]
              Perform Find[]
              If [ Get ( FoundCount ) = 0 ]
                 Exit Script[]
              End IF

              • 4. Re: Time-Dependent Automated Email Script
                RobMad

                Hello all - thank you so much to those who offered some advice.  I used your comments to write the below script.  I hope it makes a lot more sense now.  I have not left anything out, so please let me know if I need to add something more - I've never written anything before so any tips are welcome and helpful!

                 

                Loop

                     Set Error Capture [On]

                     Perform Script ["CheckEmail"]

                     Enter Find Mode [Restore; Pause]

                     Set Field [Admin::DateAppointment = (Get(CurrentDate) + 10)]

                     Perform Find []

                     If [Get(FoundCount) = 0]

                          Exit Script []

                     End if

                     Set Error Capture [Off]

                     Go to Record/Request/Page [First]

                     Send Mail [Send via SMTP Server; No Dialog; To:blahblahblah...]

                     Go to Record/Request/Page [Next; Exit after last]

                End Loop

                • 5. Re: Time-Dependent Automated Email Script
                  philmodjunk

                  I would move lines 2 - 8 up to be outside of the loop. As far as I can tell, you only need to perform the find once to set up your found set and then you loop through those records.

                  This script step has som extra settings that don't appear to be needed:

                  Enter Find Mode [Restore; Pause]

                  At the very least, clear the pause check box. If there is criteria specified inside this script step (Restore indicates that you do), this will work, but I prefer to not do this and use more set field steps to specify any additional criteria. It saves time when I examine a script a month later as I don't have to remember the criteria and don't have to double click the script step to see the criteria used.