1

I've got a complex process that takes about thirty seconds to execute, depending on how many records are in the org. I can estimate the completion percentage by iterating a count variable in my for loop pretty easily, but I'm unsure of how I should approach displaying it.

Here's an example:

public void executeLongRunningProcess() {
    Integer progressIndicator = 0;
    sObject[] myObjectsList = getObjectsToProcess();

    for (SObject aRecordName : myObjectsList ) {
        // Some complicated processing method that consumes a heavy amount of cpu time

        progressIndicator++;
    }
}

A few thoughts:

  • Would making my method calls asynchronous be an obvious first step?
  • I'm just getting into Vue.js. Could I take advantage of the field-binding to avoid making a call to my controller every 500ms or so?
  • Would Visualforce's ActionPoller/ActionSupport be something I should check out?
3

Anything you do inside your Apex Code won't update the user interface. As such, you might want to get your initial estimate in JavaScript (via an API call), then as you process records, you can update the UI. If you've seen my ID key prefix scanner, which scans across all 238,328 possible key prefix values to try and sniff out hidden objects, you'll see it takes about 30-40 seconds, where I display a progress bar to let the user know how far I've progressed. You can install this your production/developer org or your sandbox org if you'd like to see how it works.

4
  • Excellent suggestion! So as an easy solution, maybe this would work? Integer recordCount = [My Query].size(); // Each iteration takes 500ms, so multiply recordCount by 500 to get a ballpark number for run time.
    – Isaac L
    May 24 '18 at 20:50
  • @IsaacL yes, for estimated time, it'd be pretty practical. If you expect more than 50k rows, you can use "ReadOnly" annotation on a method to query up to 1,000,000 rows, then you could determine how many chunks you need, etc. The actual processing would be a series of RemoteAction calls, and the Visualforce page itself can collect the results. Feel free to check out my package in a sandbox or something to see how I did it (it's unmanaged, so you can see the code).
    – sfdcfox
    May 24 '18 at 20:52
  • Great explanation of chunking the data. I'll have to see how ambitious I get with it, but I appreciate the code sample. One more thought - do you imagine it would be possible for me to execute the long running method asynchronously, then perform a remote call on the variable every 500ms or so? I'm guessing that wouldn't work, simply because it's running in static context.
    – Isaac L
    May 24 '18 at 21:17
  • @IsaacL You can't get the results back directly from the static code, but you can do your computations and then save the results in a file (e.g. an Attachment or Document), and retrieve it when it's done.
    – sfdcfox
    May 24 '18 at 22:45

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.