We're using Omni-Channel to route leads to specialists, and have a requirement that certain leads come before others. In this case, we're relying on a custom field that is, in essence, a priority score.
Based on conversations with Salesforce Support, the only way to change the order of a queue is to set the ownership of the queue members. Thus, to re-order a queue, we must first reassign all queue members to a holding queue, then pick out the ones we want and assign them in groups. For example, after reassigning to the holding queue, we pull all the priority 1 leads, then all the priority 2, and so on.
The problem I'm running into is that we have to use queueables as part of any job that updates Leads, because we use callouts to an external service, and (as I recall) that's a no-no in batch jobs.
Thus, my pattern is something along these lines:
- Get a list of all leads of a particular type (campaign, in this case)
- Batch those leads up
- For each batch, fire off a queueable to reassign to holding
- In the
finish
method of the batch job, fire off the next step
My problem is this: I can't guarantee that all of the queueables finish by the time the finish
method of the parent batch job fires. That means I might actually have some leads that have not been assigned to holding yet, and would missed getting processed.
I played with adding a check method in between the two jobs, to see whether the main queue was really empty, but it has to run in what is in essence an infinite loop; I added a counter to get around this, but had to add an artificial delay to make that work, and now I'm running into Apex CPU limits...so it's back to square one.
Oh, and here's the rub: I can't just load all the leads into the queue as a one-off. I have to load them by time zone, based on when they're callable, and I have to re-order them each time. (For example, when 9am CT rolls around, a lead in Dallas with a priority of 1 should be called before a New York City lead with a priority of 2, even though the NYC lead was there first.)
Thus, the question: What sort of pattern would work for this setup? Assume that I have a sufficient number of leads that I have to process them in batches of some sort (I typically have thousands, if not tens of thousands), and that I have to run the job at least three times per day.