Sendgrid-nodejs: Unable to create contacts with custom fields set

Created on 10 Jul 2019  ·  39Comments  ·  Source: sendgrid/sendgrid-nodejs

Issue Summary

Unable to create contacts with custom fields set using New Marketing Campaigns PUT
/marketing/contacts API

Steps to Reproduce

  1. Having custom fields created, send following payload to PUT /marketing/contacts
{
  contacts: [
   {
    email: '[email protected]',
    custom_fields: {
      purchaseTime: -1,
      activations: 0,
      partner: 'none',
      userCreationTime: 1544997675,
      lastSigningTime: 1544997675,
      emailVerified: 1,
      disabled: 0,
      displayName: 'Rv',
      photoURL: 'https://lh6.googleusercontent.com/-,
      uid: 'uid'
    }
  }
  ]
}

Observe
invalid custom field ids supplied - partner,emailVerified,photoURL,uid,purchaseTime,activations,userCreationTime,lastSigningTime,disabled,displayName

Technical details:

  • sendgrid-nodejs Version: 6.4.0
  • Node.js Version v12.1.0

Most helpful comment

Seems like the current API is expecting the field_id and not the name.

If you inspect the network traffic inside SendGrid when loading the https://mc.sendgrid.com/custom-fields page you can see the response for /field_definitions that returns the field_ids as well as the names.

SendGrid either needs to display this id value on the front end or update the API to accept the name in the request.

All 39 comments

+1

I ran into the same issue today. I brought it up with chat support (referencing this issue). Their response:

"Ok so this has been going on recently. You are not the only one. Unfortuantely we don't have a work around for this right now. Our senior engineers have been made aware of this issue."

Screenshot 2019-07-12 at 15 54 56

That's what I get on the official API website

I have the same problem on the official API. If you take the cURL code generation snippet it will work.

Seems like the current API is expecting the field_id and not the name.

If you inspect the network traffic inside SendGrid when loading the https://mc.sendgrid.com/custom-fields page you can see the response for /field_definitions that returns the field_ids as well as the names.

SendGrid either needs to display this id value on the front end or update the API to accept the name in the request.

You're probably right. Sad that their support could help anyone at all. And, of course, using ids makes api much more difficult to use.
Did you notice if custom fields endpoint returns id when you create a new field, or you need to GET it?

@AAverin Not too sure since I created the custom fields in the GUI. The API reference says it returns the id in the response object for PUT/PATCH requests though.

Support told me it's possible to use custom fields names by passing them directly in the payload, without extra custom_fields object.
It worked, but values for fields were not set.
Anyways, custom fields are only needed for segments and there are no segments in New Marketing. So maybe just skip custom fields completely?

So just to close the issue.
Correct payload would be:

{
  contacts: [
   {
    email: '[email protected]',
    purchaseTime: -1,
      activations: 0,
      partner: 'none',
      userCreationTime: 1544997675,
      lastSigningTime: 1544997675,
      emailVerified: 1,
      disabled: 0,
      displayName: 'Rv',
      photoURL: 'https://lh6.googleusercontent.com/-,
      uid: 'uid'
  }
  ]
}

They also promised to test and update API documentation

Any update on this?
I have the same error when I have the field name inside custom_fields.
I have no error when I use the field id inside custom_fields or inside BUT the contact is not saved.

I am also using curl directly, not this module, so I think the problem is on sendgrid side.

@soramister
Last answer I got from support was that now we are supposed to use special id's when setting values to custom fields.
See @adanot answer above.

Still, custom fields are somewhat useless at the moment because it's not possible to build custom segments. So yes, you can send custom fields but you can't really use them for anything.

@AAverin
I have tried with the field ID too. There is no error message BUT the contact does not save.

@soramister I didn't try myself yet, because due to missing Segments and some feature I consider switching to Mailerlite instead.
Anyways, according to support, you should be able to get your created custom fields using GET https://api.sendgrid.com/v3/marketing/field_definitions call.

Then you will need to take the IDs for the custom fields you are interested in adding/updating values for, and use those in your call to add/update a contact with: PUT https://api.sendgrid.com/v3/marketing/contacts

Here is example payload support gave me. keys for custom_fields field are those special ids.

{

"contacts": [
    {
        "email": "[email protected]",
        "address_line_1": "123 main st",
        "custom_fields":
            {
                "e6_T": "5555555555",
                "e5_T": "Acme Group"
            }
    }]
}

I've tried with the fields id. While the command is successful (no error returned), the contact is not added.

@soramister
Looks fine for me.
Was able to successfully pass custom fields data for all my users

@soramister it takes Sendgrid about a minute before you'll see it.

what should be the custom field id to make it work?? @AAverin, please explain your example payload.

I've tried with the fields id. While the command is successful (no error returned), the contact is not added.

Same for me. I get a job ID in the response but it seems the data isn't in sendgrid

So for me custom fields using the new contact API are definitely not working. I've tried using custom fields ID's and without. Without ID's it errors "invalid custom field ids supplied" with ID's it works/I get a success but it doesn't update the data

{ "list_ids": [ "4fb6cca6-06dc-4aa2-b7cb-851a4e3be37e" ], "contacts": [ { "email": "[email protected]", "first_name": "Test", "custom_fields": { "e1_N": "100000", "e2_N": "100000" } } ] }

It may take a while to see the contact created on sendgrid, has the contact been created?
Your object looks good though.

It may take a while to see the contact created on sendgrid, has the contact been created?
Your object looks good though.

It's really odd, the contact will only get created if I don't include custom fields. At a bit of a loss where to go from here

@Lucania001 having the same problem! Really really unimpressed with Sendgrid's API. Extremely brittle and opaque. So little insight into what is happening, and so little documentation to explain the endpoints and their behavior.

Even with the field id rather than the field name, I'm not able to update custom fields for contacts. This needs to be addressed ASAP or a customer is down the drain.

I can't even get a contact created using SendGrid's "try it out" section of thier API without a custom field. All I get is this:

{
"errors": [
{
"field": "",
"message": "invalid JSON"
}
]
}

I can't even get a contact created using SendGrid's "try it out" section of thier API without a custom field. All I get is this:

{
"errors": [
{
"field": "",
"message": "invalid JSON"
}
]
}

Yeah, the try it out doesn't work at all really. Try it through postman. You'll likely run into the same issues as the rest of us though.

Hello,

I've come back to this issue to see if it was fixed. It is not. The problem is still present.

Here what I've tried with a curl request:

curl --request PUT --url https://api.sendgrid.com/v3/marketing/contacts --header 'authorization: Bearer {{API_KEY}}}' --header "Content-Type: application/json" --data '{"list_ids":["f85956da-5696-4d8b-826d-84ac6e27cb2f"],"contacts":[{"address_line_1":null,"city":null,"country":"UK","email":"[email protected]","first_name":"Test","last_name":"TEST","postal_code":null,"state_province_region":null,"custom_fields":{"e4_T":"EN","e2_D":null,"e1_T":"custom field","e3_T":"http://google.com/"}}]}'

When I remove the custom_fields value, it works. The contact is created. If I add back, the contact is not added.
Both case returns a job id, no error.

Please let me know what to do.

I don't think anybody from SendGrid pays attention to this thread or github in general.
The only thing you can do is write directly to support on the website, usually they help.
A solution for me was to switch off from SendGrid to another service and I already have everything running there, while SendGrid still has problems creating contacts

Thanks AAverin, to summarize: To create contacts with custom_field's just know id's are not the name you give them, sendgrid applies it's own id's you can retrieve via GET https://api.sendgrid.com/v3/marketing/field_definitions

Thanks AAverin, to summarize: To create contacts with custom_field's just know id's are not the name you give them, sendgrid applies it's own id's you can retrieve via GET https://api.sendgrid.com/v3/marketing/field_definitions

As above even if I use the ID's sendgrid applies, it still doesn't work

Here is an example curl that works for me to create multiple contacts with a custom field:

curl -X PUT https://api.sendgrid.com/v3/marketing/contacts -H "Content-Type: application/json" -H 'authorization: Bearer API_KEY' --data '{ "contacts": [{ "email": "[email protected]", "custom_fields": {"e2_N":1 }},{ "email": "[email protected]", "first_name": "Joe", "last_name": "Dirte", "custom_fields": {"e2_N":2 } }] }'

@soramister I played with your curl request. Your request works if you dont include null values for custom_fields. Null entry seem to work fine for the reserved_fields but not custom_fields. Building your request dynamically so entries with null values are not included should fix your issue.

Thanks @adanot, that worked for me. Good catch!

The meta data on the custom fields object on creating a new contact should've been on the documentation. Btw, thanks @adanot and @AAverin

They've also changed the format for the custom date fields, in the legacy Marketing API's it was "10/12/2019" and now it's "2019-10-12T23:14:20Z" which is ISO 8601 Zulu Time.

Although their UI still shows the older date format which is still confusing. The good thing would be if I'm able to use custom date field's time to create segments as earlier it just allowed to put dates.

I came across this issue today. When adding a contact that includes custom fields, the api would return a job_id but the contact would never be added.

For me, the problem was that I was sending boolean values to a 'text' custom field. Sending those booleans as strings instead resolved my problem.

The Marketing API and custom_fields have been a real nightmare. The docs tell you to send in a custom_field ID when you're PUTing a new contact into a marketing list. Then, when you do, it sends back a "invalid custom_field ID" message. Nowhere in the docs have I found it say you seen to send in those weird "e1_T" style custom_field ids. Anyone else?

But...when you send in a PUT request and use the "e1_T" style custom_field ID instead of the ID that is returned when you do a GET request on the custom_fields endpoint, it works perfectly to add a new contact.

I just don't get it.

It's fragile and often not intuitive, the Sendgrid doc says "You should save the returned id value if you ever want to update or delete custom fields.", in my case the returned id values were those weird "e1_T" style, and using those works fine.. just a pain, why they didn't just setup to take the field name is beyond me.

Bellow is api to get Id for custom fields :

But send grid api allows to add/update one custom fields per request.
While passing custom fields name directly in payload it doesn't add/update the custom fields

For those who still looking for answer, example payload below:
contacts: [ { email: "[email protected]", first_name: "test", last_name: "test", custom_fields: { e7_T: "TRUE" } } ]
So custom_fields is an object, where key is your custom field id - e7_T in my case

Really bad api design, imo

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TobiahRex picture TobiahRex  ·  3Comments

prasoonjalan picture prasoonjalan  ·  3Comments

egges picture egges  ·  3Comments

agostonbonomi picture agostonbonomi  ·  3Comments

thidasapankaja picture thidasapankaja  ·  4Comments