2

Based on the Salesforce Apex API,

http://www.salesforce.com/us/developer/docs/apexcode/

it seems Apex does not have built-in Queue, what is the best way to work around the problem?

The thing I am trying to do with a Queue is lists.pollLast(), I can use List to workaround the problem like this:

lists.remove(hostLists.size() - 1)

I am trying to find out if there's cleaner way to do it.

2
  • What are you attempting to do with Queue?
    – sfdcfox
    Commented Aug 2, 2013 at 16:49
  • Invoking a @future method is sort of adding to an Apex Queue, which executes when resources are available, although you cant influence the order of execution. Commented Aug 2, 2013 at 16:51

2 Answers 2

3

There's no data type that is a queue, but you could write your own. Your solution is the most efficient solution for LIFO natively. You could also use list.add(what, where) to add items to the beginning, and list.remove (where) with the value 0.

2

I prefer using a custom object as a queue - or using custom fields on a standard or custom object to create a virtual queue.

For example, let's say I just subscribed to a service that will add 1000 leads to my Salesforce instance per month. Each morning I want 10 leads to be assigned to each sales person in a particular group in a round-robin fashion. I could create a virtual queue on Lead by creating a custom field such as Assigned_To_Sales_Rep__c (Checkbox) which defaults to false. Then once per day a scheduled process wakes up and creates an in-memory queue for batch Apex to process with a SOQL statement such as:

[SELECT Id, Name, ... FROM Lead WHERE Assigned_To_Sales_Rep__c != true]

Then once each lead is assigned, Assigned_To_Sales_Rep__c gets set to true which effectively removes it from the "queue."

Alternatively you could create a custom object (called Queue or something) that gets populated with data as work comes in. Populating this queue could be trigger-based but regardless of how records get in the queue, some process either wakes up (scheduled Apex) or is kicked off manually and processes the queue items, deleting the individual records from the queue object when processed.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .