How to get the previous value of a deal in a webhook

I’m trying to get the latest dealstage property of a deal in a webhook. So i can compare the previous value with the new value received in the webhook.
I do this by querying the current

const properties = ['dealstage'];
client.crm.deals.basicApi.getById(dealId, properties,properties)

Output of the deal:

SimplePublicObjectWithAssociations {

 createdAt: 2024-12-25T11:11:52.478Z,

 archived: false,

 propertiesWithHistory: { dealstage: [ [Object], [Object], [Object] ] },

 id: '30926967573',

 properties: {

 createdate: '2024-12-25T11:11:52.478Z',

 dealstage: 'presentationscheduled',

 hs_lastmodifieddate: '2025-01-11T16:47:42.235Z',

 hs_object_id: '30926967573'

 },

 updatedAt: 2025-01-11T16:47:42.235Z

}

output of the history:

[

 {

 value: 'presentationscheduled',

 timestamp: '2025-01-11T16:47:37.325Z',

 sourceType: 'INTEGRATION',

 sourceId: '6081191'

 },

 {

 value: 'qualifiedtobuy',

 timestamp: '2024-12-26T14:30:56.631Z',

 sourceType: 'CRM_UI',

 sourceId: 'userId:75676137',

 updatedByUserId: 75676137

 },

 {

 value: 'appointmentscheduled',

 timestamp: '2024-12-25T11:11:52.478Z',

 sourceType: 'CRM_UI',

 sourceId: 'userId:75676137',

 updatedByUserId: 75676137

 }

]

Now I wanted to sort the history by timestamp, filter out the current value based on the timestamp of the deal, and then get the first remaining history entry. This should give me the previous value.

However, i notice that the timestamp do not match: timpestamp of deal: 2025-01-11T16:47:42.235Z and timestamp in history: '2025-01-11T16:47:37.325Z’. There is ± 5 second difference.
So my question is, what is a robust way to get the previous value in a webhook? Is it safe to sort the history entries and get not the first (as this is the current value), but the second entry?

Hello! @Jdb8,

Here’s what I do to get the previous dealstage. Since HubSpot’s propertiesWithHistory already returns the sorted results, we can simply use dealstage[0] to get the current stage, and dealstage[1] to get the previous stage.

const main = async () => {

 const response = await axios.get("/crm/v3/objects/deals/<dealid>?propertiesWithHistory=dealstage");

 console.log(response.data.propertiesWithHistory.dealstage[1])

};

WesQ_0-1737791804561.png
Was I able to help answer your question? Help the community by marking it as a solution.