Associations API has started sometimes returning 400 Bad Request
SOLVE
The associations API has started sometimes returning 400 Bad Request to our code.
The code runs when a new user is registered in our application. First, we look up the contact by e-mail. If there's no contact, we create one. Next, we PATCH some changes to the contact whether it existed or not.
Then, we attempt to look for company records associated with the contact. This is the part that's failing. The request is a simple GET:
GET /crm/v3/objects/contacts/NNNN/associations/company
where NNNN is the HubSpot ID of the contact record, which may have just been created at the time of the call.
When we get 400 Bad Request we log the ID it was trying to fetch, and if I later call the same function from a unit test, with the same ID value, it works. So it seems there's some time-dependent failure happening when looking up associations for a newly-created contact.
Perhaps the code that automatically creates company records for new contacts is clashing with the associations API service?
What's the right thing to do to avoid the problem?
Associations API has started sometimes returning 400 Bad Request
SOLVE
I tracked down the whole HTTP response in the logs, and it turns out the error message had been misidentifying which operation had failed. I now know what's going on, and how to fix it. Not a problem with associations after all! Sorry for the misinformation.
There is a problem with timing when creating contacts and then trying to fetch associated companies, but it's fixable by making the client pause for a second or two to let HubSpot finish what it was doing. (The whole sequence is running synchronously.) I'd already put that pause in, but I was wondering why it was still failing.
Associations API has started sometimes returning 400 Bad Request
SOLVE
I tracked down the whole HTTP response in the logs, and it turns out the error message had been misidentifying which operation had failed. I now know what's going on, and how to fix it. Not a problem with associations after all! Sorry for the misinformation.
There is a problem with timing when creating contacts and then trying to fetch associated companies, but it's fixable by making the client pause for a second or two to let HubSpot finish what it was doing. (The whole sequence is running synchronously.) I'd already put that pause in, but I was wondering why it was still failing.
What programming language are u using? Is your programming language asynchronous or synchronous?
What is the exact response you get on the associations API? In the response body is a message with the error.
You are talking about code that creates a company for new records. That's code from your own application i guess?
At this moment, with the provided information I think you have some timing issues in your application. Requests are maybe not finished yet or something like that.