A quick way that comes to mind would be to loop through the records, creating and sending the pdf on each record
First, isolate the set of records and perform something anlong the lines of:
Set Variable [$temp; Value: Get ( TemporaryPath )
Save Records as PDF [Restore; No dialog; "filename.pdf"; current record]
Send Mail  (set email parameters, attach file using variable and filename for the path and address from record)
(do some error checking to see if the email was successful, if so move to next record, if not display error, etc..)
End Loop [ exit after last]
The above assumes that the pdf is different for each record. If it is the same pdf then you would obviously want to remove the Create PDF step from the loop and define the pdf before the loop.
Matt has given you one option, but there are others....
The script step "Send Mail" allows you to create a script that sends multiple emails without looping through the records. If you choose the option "Multiple Emails (one for each record in the found set)" and choose the email field as the email "To" address, that will send out multiple emails.
In order to attach the pdf, you'll also need to run the script that creates the pdf first, and use the Send Mail script step to attach the pdf to the email using the file path of the saved pdf.
You also have the option of using the users email client or an smtp account to send the emails directly from FileMaker. If you are doing a "mass" email - the smtp option may be preferable, since it won't create a bunch of sent mail in the users mail application. Also, to use the email client - the user must have an email client configured on the computer, and that is not always the case.
AND - there is a similar thread about sending individual emails with a pdf attached that has a sample file you can download with script attached. you can probably use this script with some modifications...
Thank you both for your suggestions but I am still not able to get the records to advance before sending the 2nd email with the new PDF. The emails have the same PDF instead of the next records' PDF. Here is the script I am using, I have tried various updates to the script without success. I would appreciate any other suggestions. Thanks. Faith
Added attached script.
Email Script.PNG 18.2 K
1 of 1 people found this helpful
Remove the export records, you are already doing an "export" when you save the PDF.
You can also remove the exit look if statement as the end loop already qualifies the loop to end when you hit the last record.
Also, try saving the PDF with a different name each for each record. I have run into numerous issues with windows where FileMaker didn't want to overwrite an existing file. It doesn't happen always, but it happens enough to me.
In theory, with those edits, the script should be working.
When I removed those 2 steps, it runs through the records twice, I get twice as many emails as I should. I'm not sure how to save each PDF with a different name in my current script, is this a simple step?
1 of 1 people found this helpful
First, your Export Field contents does not have a path specified, so the pdf is not being deleted. That script step should look like the attachment.
Second - try putting in a pause of 1-2 seconds after the Send Mail script step and/or after the Export Field Contents script step. Even a pause of zero seconds may do it. I have found when looping through records to send pdfs, that FileMaker may perform the steps faster than the OS and mail client can save and send the pdfs. That shouldn't cause duplicate emails, but I have seen other issues, with blank pdfs in the email or missing emails that the pause step has solved. You could be overloading the OS or the email client. You should NOT need to give each record a unique name, although that is easy to do and won't affect the delete file since you are using a variable for the path.
Next, your Exit Loop If step will not work because there is no active field. In order to have an active field, the cursor must be in the field. So you would need a GoToField: Select/Perform step for each record in order to activate the field to run your test. I am not sure what you are trying to test for empty??
Last, but NOT least - you need to do some debugging/troubleshooting steps. If you have FileMaker Pro Advanced (and you should if you are doing any scripting! upgrade NOW!) turn on the script debugger and the data viewer and see what is happening as you step throught the script. You should also debug the Send Mail step using a dialog. Turn the dialog back on, and start with a small found set, say, 10 or so records, then see what happens with each one in order BEFORE the email is sent.
Thank you for your helpful advise. I have now limited the script back to just 6 steps. I tried adding pauses but it does not change the result. I was able to get to the correct number of emails but the pdf will not change with each record. It seems like the export field contents is not working. I have a request in to upgrade to the Advanced version so I can use the debugger. I did turn the dialog on but those seem to work just fine. At this point I think it may be best to wait and see if that will help me find why the pdf's don't update. Thanks again. Faith
Now it sounds like the problem may be with the PDF layout itself. Can you explain what is on the layout that is not updating? Are they fields? What is the update supposed to do??
Maybe post a screen shot of your layout setup with field names showing?
The PDF is a report that should just advance through the records. I am able to create the PDF's without any issues. Does this make sense? I think it is pretty straight forward.
Yes, makes sense. Looks like these are all local fields to this layout table? So what is happening is that you are getting an email for each record, with a pdf attached, but the same pdf each time? So then you ARE having issues with creating the pdfs. Just because you can create a PDF manually, doesn't mean you won't have problems with a scripted creation.
You said that the Export Field Contents is not working - so are you getting any error messages? Have you suppressed the error messages? (Allow User Abort Off, Set Error Capture On) Try turning those steps OFF - my guess is there is some kind of error being thrown when the pdf is being created or the export field contents is running that is preventing them from working properly.
By the way - Export Field Contents with no field selected WILL throw an error, but it should still delete the file. SO - Try this with error capture on and a found set of TWO. See what happens when it goes to record 2.
Then try renaming the pdfs so you pull part of the name from the current record, so instead of "ClassifiedReport.pdf", it would be named "SmithClassifiedReport.pdf" or "JonesClassifiedReport.pdf" Then you can really see if you are creating and deleting the pdfs the way you want.
Welcome to 80% of the developer's life - painstakingly debugging everything you do!!
It seems to me that you ARE opening the same old PDF every time...
You have the variable set to the desktop path... and you use it in the export... but you are not using that same path for the Save as PDF... but rather it is saving next to the file you are working on...
Why are you exporting as a PDF when you are already saving as a PDF... ?
What "Field contents" are you exporting? There is no nominated container field and nothing being put there by the script anyway...
Get rid of the export line and change the filepath for the email attachment to $ClassifiedReport.
I missed that she was not using the file path variable in the Save as PDF step - Lyndsay - you are so right..
But - the Export Field Contents step does not specify a field on purpose. This step should delete the file from the desktop. It's not working because she is not using the correct path in the SaveAsPDF step.
So she should NOT get rid of the Export step, she SHOULD use the path variable in the Save as PDF step
So the script would then be:
Set variable $ClassifedReport to Get(DesktopPath) & "ClassifedReport.pdf"
Save as PDF $ClassifedReport (saves the record as a pdf to the path specified)
Export Field Contents [ $ClassifedReport ] (deletes the "ClassifiedReport.pdf file from the Desktop)
I would probably have used a different variable name - like $filepath, so this would be less confusing...
So nice to have a fresh set of eyes looking at this!
I think it is a good idea to think of saving as a pdf as a form of printing. From what you have said so far it seems to me that you want to:
1. Step through a found set of records, and on each record ...
2. "Print" a report, as a pdf, and send that report to someone the record is relevant to
3. ... but not retain the pdf that was sent
If that is right, I assume that you also ought not be left with the last pdf on your desktop, which will contain data from the last record visited. You may wish to consider exporting a blank one at the end of the script so as not to leave data sitting round outside your database.
The following script achieves this:
Enter Browse Mode
Go to Layout [ “TestFile” ]
Go to Record/Request/Page [ First ]
Set Variable [ $ClassifiedReport; Value:Get ( DesktopPath ) & "Classified Report.pdf" ]
Save Records as PDF [ File Name: “$ClassifiedReport”; Current record ]
# NOTE: if you run the script to send email without dialog, the pause is needed to give your OS time to complete the save
before it is sent
Pause/Resume Script [ Duration (seconds): 2 ]
Send Mail [ Send via E-mail Client; To: "your client email address"; Subject: "Severance Report Testing"; Message: "Did you
get one emailed report with your data?"; Attachment: “$ClassifiedReport” ] [ No dialog ]
Go to Record/Request/Page [ Next; Exit after last ]
# NOTE: as the last pdf contains data from the last record visited, you may wish to consider exporting a blank one at the end of the script so as not to leave data sitting round outside your database. The following sequence will achieve this
Set Variable [ $ClassifiedReport; Value:Get ( DesktopPath ) & "Classified Report.pdf"
Save Records as PDF [ File Name: “$ClassifiedReport”; Blank record, as formatted ] [ Restore; No dialog ]
NOTE: the Set Variable formula saves each report under the same name, crunching the previous version each time. If you want to save each pdf as a separate file you could include the recordID and perhaps a timestamp, such as:
Get ( DesktopPath )& TestFile::~recordID & "Classified Report" & Get ( CurrentTimeStamp ) & ".pdf"
Hope that's of some help.