Bad Request - adding Engagement

SOLVE
Highlighted
Occasional Contributor

Hi all


Newbie here.

 

I'm writing an app to import GoldMine history into Hubspot as completed tasks.

 

I've managed to, as a test create a company, so I know I'm not far away but when I try and add an egagement task I just keep getting Bad Request.  Quite frankly this is f'ing useless information but it is what it is.

 

Here is an example json string I'm building from a SQL query to the GoldMine database:

 

{
"engagement": {
"active": true,
"ownerId": 1,
"type": "TASK",
"timestamp": 130181149200000000
},
"associations": {
"contactIds": [
1
],
"companyIds": [],
"dealIds": [],
"ownerIds": []
},
"metadata": {
"body": "\r\n\r\n \r\n\r\n\r\n\r\n\u0000",
"subject": " (oc:Aurangzeb Khan)",
"status": "COMPLETED",
"forObjectType": "CONTACT"
}
}

 

Here is my code:

 

Dim newTask As New newTask
Dim onDateTime As DateTime = contHist("ondatetime")
Dim newEngagement As New Engagement
With newEngagement
.active = "true"
.type = "TASK"
.timestamp = contHist("ONDATETIME").ToFileTime
.ownerId = 1
End With
Dim newAssociations As New Associations

With newAssociations
.contactIds = {1}
.companyIds = {}
.dealIds = {}
.ownerIds = {}
End With
Dim newMetaData As New Metadata
With newMetaData
.body = contHist("ConvertedNotes")
.subject = contHist("REF")
.status = "COMPLETED"
.forObjectType = "CONTACT"
End With
With newTask
.engagement = newEngagement
.associations = newAssociations
.metadata = newMetaData
End With

Dim jSonNewTask As String = JsonConvert.SerializeObject(newTask, Formatting.Indented)
Dim contactContent As New StringContent(jSonNewTask, Encoding.UTF8, "application/json")

Dim url As String = "https://api.hubapi.com/engagements/v1/engagements?hapikey=myapikey"

Dim client As HttpClient = New HttpClient
client.BaseAddress = New Uri(url)
client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))

client.MaxResponseContentBufferSize = 2147483647

client.Timeout = TimeSpan.FromMinutes(5)

Dim response As HttpResponseMessage = client.PostAsync(url, contactContent).GetAwaiter.GetResult

If response.IsSuccessStatusCode Then
Dim a As String = response.ReasonPhrase
End If

Reply
0 Upvotes
2 Accepted solutions

Accepted Solutions
HubSpot Moderator

Welcome, @Deadeye.

 

Happy to help.

 

Right off the bat, I see a few issues with that payload:

  • The timestamp of 130181149200000000 is improperly formatted. Per this article, HubSpot's APIs only accept UNIX millisecond timestamps.
  • The ownerId of 1 and contactIds value of 1 will fail unless you have owner and contact records in your account with exactly those IDs, which do not exist in any account. For the contactIds array, I recommend including a vid value returned after creating a contact. For the ownerId value, input one of the IDs returned from this endpoint.

Isaac Takushi

HubSpot Developer Support
HubSpot Moderator

Hi, @Deadeye.

 

The timestamp value 1550750520 only corresponds to 2/21/2019 at 12:02 PM (local time) if the calculator uses seconds. If you add three zeros to the end, you will get 1550750520000, which is the same date and time in milliseconds.

Isaac Takushi

HubSpot Developer Support
Reply
0 Upvotes
5 Replies
HubSpot Moderator

Welcome, @Deadeye.

 

Happy to help.

 

Right off the bat, I see a few issues with that payload:

  • The timestamp of 130181149200000000 is improperly formatted. Per this article, HubSpot's APIs only accept UNIX millisecond timestamps.
  • The ownerId of 1 and contactIds value of 1 will fail unless you have owner and contact records in your account with exactly those IDs, which do not exist in any account. For the contactIds array, I recommend including a vid value returned after creating a contact. For the ownerId value, input one of the IDs returned from this endpoint.

Isaac Takushi

HubSpot Developer Support
Occasional Contributor

Hello Isaac

 

Thank you for your response.

 

I updated the contactIds and ownerId and have ensured the timestamp is in the correct format.  I now receive a 200 OK which is great and my actions appear in HubSpot.

 

However, despite my correct timestamps (I check them on https://www.unixtimestamp.com) all the tasks I add are set to 18/01/1970 23:45.

 

Here is an example timestamp: 1550750520 which is 02/21/2019 @ 12:02pm however the due date of the task in Hubspot is 18/01/1970 23:45.

 

Thanks again for your help

Reply
0 Upvotes
HubSpot Moderator

Hi, @Deadeye.

 

The timestamp value 1550750520 only corresponds to 2/21/2019 at 12:02 PM (local time) if the calculator uses seconds. If you add three zeros to the end, you will get 1550750520000, which is the same date and time in milliseconds.

Isaac Takushi

HubSpot Developer Support
Reply
0 Upvotes
Occasional Contributor

Isaac, another great response, thank you.

 

I have another question if I may - if I should raise a seperate thread then of course I will.

 

Is it possible to get a contact from a new field I created and imported into using the standard import tool or am I limited to finding a contact by email?

Reply
0 Upvotes
HubSpot Moderator

Hey, @Deadeye.

 

Would you actually mind posting again for that question? I'll try to reply later this afternoon.

 

For organization and ease-of-use, we usually try to keep threads focused on a single topic or issue.

 

Thanks for your understanding!

Isaac Takushi

HubSpot Developer Support
Reply
0 Upvotes