Sendgrid with Dynamic Templates Missing Data

We're using Sendgrid v3 /mail/send to trigger warning emails to our team about events. However, I can't get the data to appear in our emails.

Our workflow goes like this

  • cron trigger (startTrigger)
  • postgres query returning tuples with fields name and amount (getLowBalanceBountiesQuery)
  • execute JS to format the data for sendgrid (fmtSendgrid)
  • use sendgrid resource query to send request to their API (sendEmail)

Our fmtSengrid block executes the following (some fields modified here for privacy):

return JSON.stringify({
  template_id: "d-our-template-id",
  personalizations: [
    {
      to: [
        {
          email: "email@email.org"
        }
      ]
    }
  ],
  from: {
    email: "email@email.org"
  },
  // this is the key part that is not working
  dynamic_template_data: { bounties: getLowBalanceBountiesQuery.data}
})

We then use {{fmtSendgrid.data}} as the body for sendEmail.

I've been able to confirm that:

  1. getLowBalanceBountiesQuery.data has the expected records in it (i.e. we aren't running fmtSendgrid on a falsy)
  2. Sendgrid delivers the correct dynamic template email to the recipient but with no dynamic data
  3. Whether or not we use JSON.stringify() in our formatting function, the resulting email contains no data.

For reference, we have Sendgrid test data that renders successfully in our dynamic template. Here's the template:

<!-- Template -->
{{#if bounties}}
<ol>
  {{#each bounties}}
   <li>{{{this.name}}}: <b>{{{this.amount}}} remaining</b></li>
  {{/each}}
</ol>
{{else}}
    <p>No bounties</p>
{{/if}}

and our Sendgrid test data:

{
    "bounties": [
        {"name": "Sendgrid test 1", "amount": 3500},
        {"name": "Sendgrid test 2", "amount": 2300}
    ]
}
    

I think the issue is the way the data is being serialized, but I also have a suspicion that the JS that creates the JSON that is transmitted to Sendgrid may not have been evaluated, because sometimes the hover over {{fmtSendgrid.data}} displays as null, even though our the JS was executed on data from a successful database query.

How can we reconcile the data passed to dynamic_template_data with the test data we're using in Sendgrid?

Hey @jev!

Is this still an issue you're running into? If so, I'd be curious to hear more about how you have your sendEmail block configured. Have you already explored using the SendGrid integration?

With that, you shouldn't need to use JSON.stringify() on your template data:

If you're using a REST block or something similar instead then screenshots may be helpful!

Thanks for your reply! I am using the Sendgrid integration, but the block input I see doesn't look like the screenshot you shared.

It seems like my input block can only handle data for Mail body compression? I don't have any other input fields, and the toggle to JSON functions don't do anything.

Do you think I'm using the wrong resource type in my workflow?

Ah it looks like you're using the raw body, if you click here do you see the other UI?

That being said, templates should also work with raw bodies :sweat_smile: can you try moving the dynamic template data into your personalizations object and also return the object without stringifying it?

return {
  template_id: "d-our-template-id",
  personalizations: [
    {
      to: [
        {
          email: "email@email.org",
        },
      ],
      dynamic_template_data: { bounties: getLowBalanceBountiesQuery.data },
    },
  ],
  from: {
    email: "email@email.org",
  },
};