'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 = "[email protected]";
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": "[email protected]",
"name": "Blah blah"
},
"personalizations": [
{
"to": [
{
"email": "[email protected]",
"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):
- Cannot use dynamic template data with a legacy template ID -
I'm not using a legacy template id according to the UI - The template_id must be a valid GUID, you provided '[d-xxxxxxxxxxxxxx]' -
I sent what I was given on the UI. - 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 - 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
Solution 1:[1]
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.
Solution 2:[2]
I was getting the same error. I had failed to copy the Template ID correctly. The Template ID is displayed when you click in the sidebar of the SendGrid control panel, "Email API ? Dynamic Templates". It is not the same as the ID you get from the /v3/designs endpoint, alas.
The error message is ambiguous. It says "Cannot use dynamic template data with a legacy template ID", when in fact it should say "Template ID not found". I was misled into believing that my Template was a legacy version, which it was not. Four hours I'll never get back.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | philnash |
| Solution 2 |
