2

I have a managed package app with a CustomObject__c where some of its fields need to be "synced" with an external web service. To stay within technical limits the Sync callout is only done once a day.

I now want to find out which records of CustomObject__c have changed in certain relevant fields (So LastModifiedDate won't work) since the last callout and only sync those in the next run.

Is there anything in Salesforce that I can use for that? I was thinking about using History Tracking, or a custom hash which I calculate on Trigger.

  • 6
    You've basically answered your own question. You'd have to use history tracking, write a trigger to keep track of specified field changes, or build a process builder/workflow to set some sort of flag. – sfdcfox Jul 9 at 12:59
  • @sfdcfox Didn't even know that this works. To get started easier...can you point me to any documentation example. Even if this works I am still not sure if that is a good solution. is History Tracking relyant. Can I package it and can subscribers deactivate it? – Robert Sösemann Jul 9 at 13:56
5

Field History Tracking is the most obvious choice, but comes with significant caveats:

All that said, the implementation would be pretty straightforward - a schedulable batch class that queries CustomObject__history based on a CreatedDate newer than the last run, and compiles and sends the changes to the external webservice.

  • 2
    N.B. testmethods for xxx__history objects are difficult as OldValue and NewValue can't be mocked ; requires mockable wrapper classes – cropredy Jul 9 at 16:56
  • Thomas would you mind adding a short code snippet to make this answer complete. I guess you have something flying around ;-) I'd really like to see how you efficiently compare states. – Robert Sösemann Jul 10 at 10:01
1

Maybe this Summer'19 improvement can help you : we can make an async trigger with Change Data Capture https://developer.salesforce.com/blogs/2019/06/get-buildspiration-with-asynchronous-apex-triggers-in-summer-19.html

For a solution without code, you can subscribe to the change events with a flow, and then call your web service https://developer.salesforce.com/docs/atlas.en-us.platform_events.meta/platform_events/platform_events_subscribe_flow.htm

Please take into consideration the limits here : https://developer.salesforce.com/docs/atlas.en-us.platform_events.meta/platform_events/platform_event_limits.htm

Correction : As you need to do the sync only once a day, with a trigger or a flow, you still have to use a flag to indicate that the record have to be synced.

  • 1
    CDC is more suitable when the external system understands that "protocol". But in my case the other system is as it is and doesn't understand th CDC format. – Robert Sösemann Jul 9 at 14:16
  • 2
    @RobertSösemann CDC can be implemented in an Apex trigger (e.g. AccountChangeEvent), which can then apply the appropriate logic for callout or setting a flag. I think this should work right even in an ISV context. – sfdcfox Jul 9 at 14:24
  • @sfdcfox why would I need CDC for that? I could even use a regular AfterUpdate trigger and set a flag on relevant changes, right? – Robert Sösemann Jul 9 at 14:25
  • 2
    @RobertSösemann The slight advantage of CDC is to avoid some additionnal execution time during the update of your custom object. The trigger (or the flow) can be executed asynchronously. – Mael Monnier Jul 9 at 14:36
  • @sfdcfox: Cool that's right. I didn't think of that. – Robert Sösemann Jul 9 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.