APIs & Integrations

SakshiChauhan
Participant

Getting error while searching/filtering contact object with Lastmodifieddate filed.

SOLVE

I'm using Filter, sort, and search CRM Objects API to filter contacts based on the Lastmodifieddate field and getting an error in response from API ( URL: "https://api.hubapi.com/crm/v3/objects/companies/search?hapikey=" )

In the contacts record, there is more than one record which has Lastmodifieddate. Tried with different filters like the first-name and it's working fine with first-name and other filters.
below is the request body I'm passing, 

request.JPG
and below is the response I'm getting from API
response.JPG

It should give me records matching with Lastmodifieddate but it's giving me an error.
The same request is working with filters like first-name, 
hs_object_id.
Please point me to the right path, is there anything that I'm missing? Or filtering with Lastmodifieddate and updatedAt is not supported?

please refer to below Snapshot which supports me that I've records ( contact records ) that have the Lastmodifieddate field with its value.
prove.JPG



0 Upvotes
1 Accepted solution
asaeljb
Solution
Member

Getting error while searching/filtering contact object with Lastmodifieddate filed.

SOLVE

Hi everyone,

 

I have been working with the HubSpot API as well and these are my findings:

 

To convert a date to an epoch unix timestamp you can use this simple JavaScript code:

 

var originalDate = '2020-10-13T15:47:31.233Z'; //original date obtained from any record
var epochDate = Date.parse(originalDate);
var newDate = new Date(epochDate);
console.log(epochDate);
console.log(newDate.toUTCString());

 

You can use the value of the epochDate variable to retrieve the records you want from HubSpot API, the format of the epoch timestamp should be something like this 1602604051233.

  • newDate variable is just to verify that the original date does not get affected after the conversion.

I have been using the Search API method for Contacts by calling the following URL:

 

https://api.hubapi.com/crm/v3/objects/contacts/search?hapikey=xxxx

 

And using this body:

 

{
    "sorts": [
      {
        "propertyName": "lastmodifieddate",
        "direction": "ASCENDING"
      }
    ],
    "filterGroups":[
      {
        "filters": [
          {
            "propertyName": "lastmodifieddate",
             "operator": "GTE",
             "value": "1602604051233"
          }
        ] 
      }
    ]
}

 

As shown above, GTE is being used as the operator for that request, but I also tested with LT,LTE, and GT and these operators work fine as well.

 

Hope it helps, best regards!

Thanks,

Asael

 

 

 

 

View solution in original post

20 Replies 20
SakshiChauhan
Participant

Getting error while searching/filtering contact object with Lastmodifieddate filed.

SOLVE

Thanks @asaeljb  @dennisedson, for providing the solution. I am marking this post as resolved.

asaeljb
Solution
Member

Getting error while searching/filtering contact object with Lastmodifieddate filed.

SOLVE

Hi everyone,

 

I have been working with the HubSpot API as well and these are my findings:

 

To convert a date to an epoch unix timestamp you can use this simple JavaScript code:

 

var originalDate = '2020-10-13T15:47:31.233Z'; //original date obtained from any record
var epochDate = Date.parse(originalDate);
var newDate = new Date(epochDate);
console.log(epochDate);
console.log(newDate.toUTCString());

 

You can use the value of the epochDate variable to retrieve the records you want from HubSpot API, the format of the epoch timestamp should be something like this 1602604051233.

  • newDate variable is just to verify that the original date does not get affected after the conversion.

I have been using the Search API method for Contacts by calling the following URL:

 

https://api.hubapi.com/crm/v3/objects/contacts/search?hapikey=xxxx

 

And using this body:

 

{
    "sorts": [
      {
        "propertyName": "lastmodifieddate",
        "direction": "ASCENDING"
      }
    ],
    "filterGroups":[
      {
        "filters": [
          {
            "propertyName": "lastmodifieddate",
             "operator": "GTE",
             "value": "1602604051233"
          }
        ] 
      }
    ]
}

 

As shown above, GTE is being used as the operator for that request, but I also tested with LT,LTE, and GT and these operators work fine as well.

 

Hope it helps, best regards!

Thanks,

Asael

 

 

 

 

PMartinez
Participant

Getting error while searching/filtering contact object with Lastmodifieddate filed.

SOLVE

Thanks, this is very helpful 

dennisedson
HubSpot Product Team
HubSpot Product Team

Getting error while searching/filtering contact object with Lastmodifieddate filed.

SOLVE

Well done @asaeljb 

Thanks for adding your solution here!

dennisedson
HubSpot Product Team
HubSpot Product Team

Getting error while searching/filtering contact object with Lastmodifieddate filed.

SOLVE

Hi @SakshiChauhan , I think I see the problem.  Looks like we are submitting in seconds and the endpoint is expecting milliseconds

0 Upvotes
SakshiChauhan
Participant

Getting error while searching/filtering contact object with Lastmodifieddate filed.

SOLVE

Hi @dennisedson,
1. I checked your created request on postman and getting the same 8 records for companies object but according to me, it is not correct. 

  • In the case of GTE, API returning more than zero records. (expected: should return zero records if I am using the current date.)
  • In the case of LTE, API returning zero records. (expected: should return more than zero records if I am using the current date.)
    SakshiChauhan_0-1603185550584.png

2. In case of Contact hs_lastmodifieddate not working. Search API is returning zero records or if I use lastmodifieddate without hs_ in Contacts object then it gives me data.

So please suggest

jtruty
Participant

Getting error while searching/filtering contact object with Lastmodifieddate filed.

SOLVE

@SakshiChauhan @dennisedson I can confirm I see the same issue - it appears that the GT/GTE/LT/LTE don't work properly using Unix dates as shown in the example. Should we be using a different format?

0 Upvotes
jtruty
Participant

Getting error while searching/filtering contact object with Lastmodifieddate filed.

SOLVE

I am seeing something similar - it appears LTE date math doesn't work properly on datetime fields using the unix timestamp as shown above. 0 records are always returned for me as well. GT/GTE seem to work.

0 Upvotes
MPrtenjak
Member

Getting error while searching/filtering contact object with Lastmodifieddate filed.

SOLVE

@jtruty wrote:

GT/GTE seem to work.


Not for me. By using GT/GTE I am getting the same records regardless of 'value'

0 Upvotes
dennisedson
HubSpot Product Team
HubSpot Product Team

Getting error while searching/filtering contact object with Lastmodifieddate filed.

SOLVE

Hey @SakshiChauhan 

Using the post method and this endpoint: https://api.hubapi.com/crm/v3/objects/companies/search?hapikey=xxxx and using this body (which includes the unix time stamp that you provided):

{
  "filterGroups":[
  {
      "filters": [
        {
          "propertyName": "hs_lastmodifieddate",
          "operator": "GTE",
          "value": "1603101460"
        }
      ] 
    }
  ]
}

I received 8 entries in my returned body. 

 

Hopefully, this updated code will help you out!

0 Upvotes
PamCotton
Community Manager
Community Manager

Getting error while searching/filtering contact object with Lastmodifieddate filed.

SOLVE
0 Upvotes
SakshiChauhan
Participant

Getting error while searching/filtering contact object with Lastmodifieddate filed.

SOLVE

Hey @dennisedson,
I checked search API with Unix timestamp with the below payload

{
    "filterGroups": [
        {
            "filters": [
                {
                    "propertyName": "hs_lastmodifieddate",
                    "operator": "LTE",
                    "value": "1603101460"
                }
            ]
        }
    ]
}

It always returns zero results with 200 status code, but we have more than zero records according to the provided filter.

MPrtenjak
Member

Getting error while searching/filtering contact object with Lastmodifieddate filed.

SOLVE

Ok, I have found a workaround. Sort contacts by modified date read chunks of 100 records (max allowed) until some arbitrary time I am looking for:

 

{
    "sorts" : [
      {
        "propertyName": "lastmodifieddate",
        "direction": "DESCENDING"
      }        
    ],
    "limit": 100,
    "after": 400
}
SakshiChauhan
Participant

Getting error while searching/filtering contact object with Lastmodifieddate filed.

SOLVE

Hey, @dennisedson  thanks for the update.
but can you please help me with how can I convert modified_date into Unix timestamp?
Below is my modified date of one record that I wanted to search by HubSpot API.

 

"2020-09-11T19:42:24.233Z"

 

 

0 Upvotes
dennisedson
HubSpot Product Team
HubSpot Product Team

Getting error while searching/filtering contact object with Lastmodifieddate filed.

SOLVE

hi @SakshiChauhan , you will need to build this into your program. 

Here is an article that may help.

For what it's worth, I have brought this up to the team to consider updating so the methods return a consistent value.  Hopefully this will be updated sooner than later!

0 Upvotes
MPrtenjak
Member

Getting error while searching/filtering contact object with Lastmodifieddate filed.

SOLVE

HI,

 

It does not work. Even if I translate string to UnixTime.

 

If I use lastmodifieddate and "GTE". I get all records regardless of timestamp.

If I use lastmodifieddate and "LTE" I get no records regardless of timestamp.

If I use hs_lastmodifieddate I get no restords regardless of timestamp, operator....

 

And also value can be as string or as number - it doesn't matter

 

All I need is api call which will return records that were modified after some arbitrary time. With that I can synchronize local contacts with HubSpot. Just periodically execute function which will return all records that were changed after last call. 😞

dennisedson
HubSpot Product Team
HubSpot Product Team

Getting error while searching/filtering contact object with Lastmodifieddate filed.

SOLVE

Hi @MPrtenjak , could you add the body of your request here?

Thanks!

 

0 Upvotes
MPrtenjak
Member

Getting error while searching/filtering contact object with Lastmodifieddate filed.

SOLVE

Hi,

 

I am calling REST function: 

 

https://api.hubapi.com/crm/v3/objects/contacts/search?hapikey=xxxx

 


With body:

 

{
  "filters": [
    {
      "propertyName": "lastmodifieddate",
      "operator": "GTE",
      "value": 1601544240
    }
  ]
}

 

Now you can change value to anything and you will always records. I can set value to 0 or 1601544240 or 2601544240 - it doesn't matter. I will always get all records

 

If I change GTE to LTE I won't get any records.

0 Upvotes
MPrtenjak
Member

Getting error while searching/filtering contact object with Lastmodifieddate filed.

SOLVE

Hi,

 

I am calling REST function: 

https://api.hubapi.com/crm/v3/objects/contacts/search?hapikey=xxxx


With body:

{
  "filters": [
    {
      "propertyName": "lastmodifieddate",
      "operator": "GTE",
      "value": 1601544240
    }
  ]
}

Now you can change value to anything and you will always records. I can set value to 0 or 1601544240 or 2601544240 - it doesn't matter. I will always get all records

 

If I change GTE to LTE I won't get any records.

0 Upvotes
dennisedson
HubSpot Product Team
HubSpot Product Team

Getting error while searching/filtering contact object with Lastmodifieddate filed.

SOLVE

hey @SakshiChauhan !

2 things that I am seeing

  1. When I ran the test, I needed to us hs_lastmodifieddate.  Yours appears to be missing the hs_
  2. If you convert the timestamp into its equivalent unix timestamp, I think you should be good to go!

Thanks!

0 Upvotes