Aug 08, 2023 10:47 AM - edited Aug 08, 2023 11:35 AM
Based on the wonderful scripting resources here, I wrote my first script with the Scripting extension to send an email via SendGrid with the user clicks a button field.
The email recipient is stored as a field in the People table in Airtable. The email body text is stored as a long text field in the Email Templates field.
The template contains several fields from the People table to communicate customized information to the recipient.. I surrounded the field names with double curly braces, but they are not being resolved. The sent email is just the raw text.
I think I'm missing something obvious...
The reason for doing this as a script instead of using the SendGrid extension is that the extension sends to all records in a view. I want to have a button on an interface that the user clicks as they go through their workflow. It's not something that can be automated. The button should send the customized email to just that record.
Aug 08, 2023 04:30 PM
Congratulations on writing your first script! Sorry, I don't know much scripting so I can't help you there. Hopefully somebody else will chime in below to help you figure out your script.
Alternatively, I wanted to let you know that you can fully automate this with no scripting at all by using Make's SendGrid integrations. A user can click on a button in Airtable which triggers a custom webhook URL in Make, which then sends the email for only that one record.
There is a small learning curve with Make, which is why I created this basic navigation video to help. I also provide the links to a few other Make training resources there as well. For example, to instantly trigger your Make scenarios from Airtable with a script instead of a button, check out this thread.
p.s. If you have a budget for your project and you’d like to hire an expert Airtable consultant to help you with the Make side of things, please feel free to contact me through my website: Airtable consulting — ScottWorld
Aug 08, 2023 05:11 PM
If you could just share the source around the fields and the email body, I may be able to figure something out.
Aug 08, 2023 06:09 PM
I've watched a couple of videos about Webhooks and Make and things aren't clicking for me yet. We have to use SendGrid for sending emails because of IT restrictions.
Aug 08, 2023 06:16 PM
Thanks for the offer, @Sho. I've pasted some of the code below. My script reads the email body text out of Airtable just fine. It's just that it doesn't populate the values of the fields from the People table. I suspect it may be because the email template is in a different table than the fields themselves, but I don't know how to solve that as a noob.
Aug 08, 2023 06:31 PM
Hi @Jean_Thoensen, Sounds like @Sho will be able to help you out with your script. And if your IT department will let you use Make (Make has very strict security protocols), I can help you get it setup to send your emails through SendGrid.
Aug 08, 2023 09:18 PM
For example, if you want to display the name in the body text
Enter <Name> for the name part of "Body Text" in "Template".
And change the code like this
// Find the text of the email template within the template record
let emailTemplateBodyText = queryResult.getCellValueAsString("Body Text");
let record = await input.recordAsync('Pick a record', sourceTable);
if (record) {
// get the name, email and message values from the record
let name = record.getCellValue('Name, First')
let emailRecipient = record.getCellValue('Email, Primary')
let emailBodyText = emailTemplateBodyText.replace("<Name>",name)
Try this and see how it works
I have never used Sendgrid, so I do not know how to use Sendgrid's API.
It seems that there is a replacement function.
Aug 09, 2023 07:09 AM
Thanks for the thoughts, @Sho. However, doing some sort of find/replace for every field in the email body is impractical and error prone. It defeats the purpose of having the template text stored in a table. I've seen screenshots of how this has been done but I'm obviously missing something!
Aug 09, 2023 07:40 AM
Those partner videos are great, @ScottWorld! Thank you.