8 Replies Latest reply on Jul 19, 2017 10:30 AM by mikebeargie

    Mass Email with different attachments

    TiffanyMulally

      Hi there. I am attempting to create a script that will send individual emails to all those found along with an attachment specific to that person.

       

      Here is what I have written so far:

      Go to Related Record [Show only released records; From table:"Student_course"; Using layout:"Student_course" (Student_course)]

      Set Variable [$filename; Value:ValueListIDs ( "file:Student Lists/Email PDF/") &Contacts::Last &"Contacts::First &".xls"]

      Export Records [No dialog; "$filename"; Unicode (UTF-16)]

      Go to Layout [original layout]

      Send Mail [Send via E-mail Client, To:...]

       

      When I run this script, it creates an email for each students along with an attachment, but it keeps attaching the same attachment (i.e. SmithSandra.xls).


      Can you please tell me how I can create and attach a separate exported spreadsheet for each students? Thank you.

        • 1. Re: Mass Email with different attachments
          mikebeargie

          This is a context issue, since you are setting $filename AFTER you go to the related table, it is showing the name of the FIRST related student back through the relationship.

           

          Simple fix, just move "Set Variable" above "Go to Related Records" and the context should be corrected.

          2 of 2 people found this helpful
          • 2. Re: Mass Email with different attachments
            philmodjunk

            You will also need to loop through your records to build this list once for each student record.

             

            And ValueListIDs does not look like the function that you should be using here. I have doubts that it produces the data that you really need here.

            2 of 2 people found this helpful
            • 3. Re: Mass Email with different attachments
              TiffanyMulally

              Thank you. That paired with the loop philmodjunk mentioned worked perfect!

              • 4. Re: Mass Email with different attachments
                TiffanyMulally

                Perfect. Thank you!

                • 5. Re: Mass Email with different attachments
                  breezer

                  You can use a loop to go through all the found set as follows:

                   

                  Go to layout ["Main"]

                  Go to Record [first]

                            Loop

                                 Go to layout ["Main']

                                      Go to related record [Show only related records...]

                                      Set variable [$filename; ...]

                                      Export Records [$filename...]

                                      Send Mail [Send via ...]

                                 Go to layout ["Main']

                                      Go to Record/Request [Next; exit after last]

                            End Loop

                   

                  Go to layout [original]

                   

                   

                  //You can also use NEW WINDOW inside the loop to maintain context

                  • 6. Re: Mass Email with different attachments
                    mikebeargie

                    Your example has some errors breezer

                     

                    Go to layout ["Main"]

                    Go to Record [first]

                              Loop

                                  Go to layout ["Main'] //Why use Go To Layout Twice? This step is redundant...

                                        Go to related record [Show only related records...]

                                        Set variable [$filename; ...] //This needs to be one line up for context, explained in my earlier post.

                                        Export Records [$filename...]

                                        Send Mail [Send via ...]

                                  Go to layout ["Main']

                                        Go to Record/Request [Next; exit after last] //Using this method results in error 101 when it exits the loop.

                              End Loop

                    Go to layout [original] //This will not go back to the layout the user was on at the beginning of the script. You just used Go To Layout a bunch of times in the loop, so it will go to the related records layout instead.

                     

                    I would clean that script up to something like this, with some added error checking as well:

                     

                    Set Error Capture[on]

                    Set Variable [ $origin ; Get(LayoutNumber) ]

                     

                    Go to layout ["Students"]

                    Go to Record/Request/Page [First]

                    Loop

                        Set variable [$filename; ...]

                        If [ Count( RelatedRecords::anyField ) > 0 ]

                             Go to related record [Show only related records...]

                             Export Records [$filename...]

                             If [ Get(LastError) <> 0 ]

                                  Set Variable [ $errors ; List( $errors ; "Export Error: " & Get(LastError) & " - " & $filename ) ]

                             End If

                             Send Mail [Send via ...]

                             If [ Get(LastError) <> 0 ]

                                  Set Variable [ $errors ; List( $errors ; "Send Mail Error: " & Get(LastError)  ) ]

                             End If


                             Go to layout ["Students"]

                        End If

                     

                        Exit Loop If [ Get(RecordNumber) = Get(FoundCount) ]

                        Go to Record/Request [Next]

                    End Loop

                     

                    Go To Layout [ Number by calculation ; $origin ]

                    If [ ValueCount($errors) > 0 ]

                        Show Custom Dialog [ "Errors Occurred During Script" ; $errors ]

                    End If

                    1 of 1 people found this helpful
                    • 7. Re: Mass Email with different attachments
                      philmodjunk

                      //Using this method results in error 101 when it exits the loop.

                      SO? Why is that a problem? It's a standard and expected use of Go to next record and you only see the error code in the server log for a server side script or if you test for it. It doesn't affect how the script executes.

                      //This will not go back to the layout the user was on at the beginning of the script.

                      Better check that one. It does go back to the layout that was current when the script started.

                      1 of 1 people found this helpful
                      • 8. Re: Mass Email with different attachments
                        mikebeargie

                        The 101 error with "Exit after last" is an issue for exactly the reason you mentioned. Seeing your server log full up with "false positive" errors detracts from identifying more severe errors. Multiply it for multiple scripts, multiple files and multiple executions and you have a lot of useless cruft clogging your log. For such an easy adjustment to one's style of developing loops, I'm surprised you're not in agreement that an Exit Loop If script step is not the correct/preferred/superior way of doing something; regardless of if it works with the error or not. I never said it wouldn't function, just that it results in the error to begin with.

                         

                        As for using "original layout" versus tracking the origin via a variable, same argument there. While your textbook definition of the script step is true, It's not always a given that "original layout" will perform with expected results once scripts start being referenced by other scripts, or the user wants to create a "back" button style of navigation without pausing a script. You yourself have suggested dynamic layout tracking in the past (link).

                         

                        I apologize for not having the textbook definition of the script step right in my earlier response, but will not apologize for trying to instill and recommend good scripting standards for context, scope and error checking.