0

I've written an apex trigger than runs when an Event is updated, and makes a callout to an external application (a slack app) to update details about that event in slack. The trigger just makes a callout with the Id.

Because of recent changes, now the external representation (in slack) of some old events is now inaccurate. I need the update trigger to run on those old events.

I want to run a query to select a group of records that would look like

Select Id from Event where RecordTypeId = '...' AND ...

with a few other filter options. I expect the set of records to be a few hundred.

I want to then force my update trigger to run on those applications. If this is not possible, can I write an apex script that will select a set of records and make a callout for each one? Ideally I'd like to set a 1000ms wait timer between each one to avoid overloading my external application if possible.

What is the best course of action?

1 Answer 1

2

Since you are working with existing data, use a tool like workbench or dataloader to export the data you want the trigger to be executed on, then upload the same data to your org using an upsert operation. This will ensure any before/after update logic is run.

If the number of records is only a few hundred and it's a one-off activity, you could run a script that follows the same approach.

Integer counter = 0; // Increment this every run
Integer batchSize = 500;
Integer offset = counter * batchSize;

Event[] myEvents = [select Id from Event where ... limit :batchSize offset :offset];

upsert myEvents;

Adjust batchSize to your own requirements (i.e. reduce it if you don't want to overload your third party service). You'll have to re-run it for as many increments as you need to cover the amount of record you want to update.

Otherwise you may want to consider writing a batch class to process thousands of records.

2
  • I see. And I could run this script from the production (ie non sandbox) dev console execute anonymous window?
    – rosghub
    Aug 16, 2020 at 18:18
  • Yes, try it from the developer console if you are able to access it in your prod environment.
    – akiradev
    Aug 16, 2020 at 18:39

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.