Emailing Search Results as a CSV File
What it does
This Add-In turns search results into a CSV file, which opens in Excel or any spreadsheet program, and emails it to the user who clicked it. The file holds the Records the user checked, or every Record the search found if none are checked. Use it when users need a list they can sort, filter, or send on, such as open invoices for a vendor or the Records due this week.
The Add-In builds the file in one pass rather than handling each Record separately, so it stays fast even for large searches.
Before you start: a holder Record
An email can only attach Documents that belong to a Record, and an Add-In run from search results has no Record of its own. So the Add-In builds the CSV on a Record, emails it from there, and then removes it. That Record can be an existing one set aside for the purpose, or a new one the Add-In creates each time it runs.
To use an existing Record, create one for this, for example with a Name of CSV Exports, and note its Record ID. Users do not need rights to this Record: the Add-In switches to it on their behalf. It can be shared by any number of export Add-Ins.
Settings
- Create a new Add-In and name it something like Email Results as CSV.
- Under General Settings, set Type to Actions Only.
- Under Where/When to Show, turn on the Search location, and use Record Types to limit it to the Record Type whose fields the CSV lists.
- In the Search options, set the option to Add-In Pre-Process and Post-Process actions will be sent all searched Record IDs as @RECORDIDS. This gives the actions the IDs of the checked Records, or of every Record found if none are checked, in the [@RECORDIDS] variable.
Actions
Add these actions under Post-Process Actions, in this order. Leave Record Actions empty.
1. Set Active Record or Create Record
If a holder Record already exists, use Set Active Record with Switch To set to its Record ID. Otherwise, use Create Record to create a new Record, and turn on Switch Context so the Add-In switches to it. The actions after this one work on this Record.
2. Search and Create CSV
- On the Search tab, set Search In to the Record Type being exported. Always set it: if the search found nothing, this keeps the action from listing every Record in the system.
- On the Advanced Search tab, set By Record IDs to [@RECORDIDS], so the file lists exactly the Records the Add-In was run on.
- On the CSV Fields tab, add one row for each column, with the column heading and the field to put in it, such as Invoice Number and [InvoiceNumber], or Vendor and [VendorName]. Turn on Add Header Row so the headings appear as the first line. Turn on Line Items only if you want one line in the file for each line item rather than one for each Record.
- On the CSV Document tab, leave Record ID blank so the file is saved to the Record from step 1, and enter a Category, SubCategory, and Name for it, such as Exports, Search Results, and Results [USERNAME]. Including [USERNAME] in the Name keeps two users running the Add-In at the same time from writing into the same file.
3. Send Email
Click Edit Details. On the Addressing tab, set Email To to [USEREMAIL], the address of the user who clicked the Add-In. On the Content tab, enter a subject and a short message, such as "Your search results are attached." On the Attachments tab, click Add Attachment, enter the same Category, SubCategory, and Name as in the CSV Document tab, and set it to Attachment Only.
4. Delete/Purge Document(s)
Enter the same Category, SubCategory, and Name under Delete Documents, leave Record ID blank, and set Delete/Purge to PURGE. This removes the file from the Record once it has been emailed. On a holder Record this matters: if the file were left there, the next run would add its lines to the end of the old file instead of starting a new one.
Using it
Run a search, check the Records to include or leave them all unchecked to include everything the search found, then click Email Results as CSV. The user receives an email with the file attached a few moments later. Large searches take longer, and the screen waits until the file has been built and sent.
Notes
- The file lists the Records in the results when the Add-In is clicked. Run the search again first if Records have been added or changed since.
- [SearchAddInRecordIDs] can be used in By Record IDs instead of [@RECORDIDS], without turning on the Search option. It always holds every Record the search found, ignoring checked Records, and is limited to the first 10,000.
- To send the file to someone else as well, add their address to Email CC, or add a Data Entry Options prompt for the address and use [FORMDATA(name)] in Email To.