'Salesforce get all objects that changed after some date

I would like to keep an updated copy of some salesforce data in a database.

e.g. a table with all contacts

However, it is impractical to truncate the table and to query all data again on a frequent basis.

Is there some way to only query changed contacts since the last sync?

e.g. I would run an hourly job that gets all contacts that changed within the last hour.

In addition, how could I deal with deleted contacts. I assume that if there is a way to get changed ones this might not include deletions.



Solution 1:[1]

To query all contacts modified since a particular date/time:

Datetime OneHourAgo = System.now().addHours(-1); 
List<Contact> AllContactsModfiedSinceDateTimeX = [SELECT Id FROM Contact WHERE SystemModStamp >= : OneHourAgo];

Same query but including all deleted contacts:

Datetime OneHourAgo = System.now().addHours(-1); 
List<Contact> AllDeletedContactsModfiedSinceDateTimeX = [SELECT Id FROM Contact WHERE SystemModStamp >= : OneHourAgo AND isDeleted = TRUE ALL ROWS]; //"ALL ROWS" must be included when using isDeleted = TRUE

Solution 2:[2]

ok, I swiched to instantWP. this is working in Firefox after network.security.ports.banned.override ==> 10080

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mark Hartnady
Solution 2 Sir Lebasi