Skip to content
Advertisement

Getting “Cannot use dynamic template data with a legacy template” with non-legacy template

I’m attempting to integrate with Sendgrid and am having a heck of a time. I have created a dynamic template – a new one, not a legacy one – with a single handle bar (first_name) in it. It has a subject. But I’m getting a load of errors that I could use some help with.

First the code:

public void sendEmail(String toEmail, String toName) throws IOException {
    String fromEmail = "validated-sendgrid-address@example.com";
    String fromName = "Blah blah";

    SendGrid sendGrid = new SendGrid("the_api_key");
    Request request = new Request();
    try {
        request.setMethod(Method.POST);
        request.setEndpoint("mail/send");
        String body = "see below...";

        request.setBody(body);
        Response response = sendGrid.api(request);
        System.out.println(response.getStatusCode());
        System.out.println(response.getBody());
        System.out.println(response.getHeaders());
    } catch (IOException ex) {
        throw ex;
    }
}

Taken almost entirely from the Java example code.

The JSON body, pretty printed…

{
  "from": {
    "email": "validated-sendgrid-address@example.com",
    "name": "Blah blah"
  },
  "personalizations": [
    {
      "to": [
        {
          "email": "stdunbar@example.com",
          "name": "Blah Blah"
        }
      ],
      "dynamic_template_data": {
        "first_name": "Babaloo"
      }
    }
  ],
  "template_id": "[d-lotsandlotsofcharacters]"
}

And then a bunch of errors that make no sense (all of which link to a 404):

  1. Cannot use dynamic template data with a legacy template ID – I'm not using a legacy template id according to the UI
  2. The template_id must be a valid GUID, you provided ‘[d-xxxxxxxxxxxxxx]’ – I sent what I was given on the UI.
  3. The subject is required. You can get around this requirement if you use a template with a subject defined or if every personalization has a subject defined. – My template has a subject
  4. Unless a valid template_id is provided, the content parameter is required. There must be at least one defined content block. We typically suggest both text/plain and text/html blocks are included, but only one block is required. – A valid template_id was provided

I’m guessing the first issue is the template_id field. It’s strange JSON in that the value includes the array open/close. Putting the value inside as text gives a parse error so Sendgrid must be taking that directly.

Any directional help would be most appreciated. The docs are rather challenging

Advertisement

Answer

Twilio SendGrid developer evangelist here.

In your example, you show the template_id as "template_id": "[d-lotsandlotsofcharacters]". The square brackets are not required in the template_id, it should just be "template_id": "d-lotsandlotsofcharacters".

The documentation for sending an email with a dynamic template does show the the template_id example as "template_id":"[template_id]" but the entire [template_id] string should be substituted for the real ID.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement