Can I create an Apex.Stack for any type?

If I will do:

Apex.Stack stackForIds = new Apex.Stack();

Then I am able to put id values in such a stack:


Here is how I tried to put an SObject into my stack:

Apex.Stack<SObject> stackForSObjects= new Apex.Stack<SObject>();
stackForSObjects.push(new SObject(Id = anId));

And it did not work out. I am getting an error:

Cannot resolve symbol Apex.Stack<SObject>

enter image description here

There seems to be no documentation on the Apex.Stack, so I would be grateful for it. Otherwise I am asking for help to figure out whether or not Apex.Stack is able to work with an arbitrary type.


As far as I know, there is no such class in the platform's API, look in your own code base for a class called Apex that has an inner class called Stack.

If you want to support other types, you will have to add additional methods for those and generalize the implementation. Other languages include better mechanisms for this situation - see e.g. Java's Generic Types.

Though as this Apex.Stack code probably does very little, changing the method signatures and implementation to use Object rather than Id would probably be the best way to go as then all types would be supported. But casts would be needed when values are popped.


Correction from sfdcfox:

There is a hidden class called Apex.Stack, and a related exception, Apex.StackEmptyException. It has a few methods available in it. It was never documented because of some "edge cases" where it would inexplicably break as well as a general lack of performance. I wasn't given any more than that, but this information came directly from someone with access to the source code for it, so it is at least accurate.

  • Oh. That explains why I was not able to find the documentation on Apex.Stack. – iloveseven Sep 9 '18 at 11:02
  • 4
    @iloveseven Yeah I'm a bit surprised you can even create a class called Apex as it makes you think it is a platform feature. – Keith C Sep 9 '18 at 11:37
  • Apex is not a recognised keyword, so I belive it should not cause compilation error. Which makes sense. – Pranay Jaiswal Sep 9 '18 at 11:50
  • @PranayJaiswal You mean a reserved word ? – Jarvis Sep 9 '18 at 13:37
  • I think the @sfdcfox answer is correct. I am able to push such code (Apex.Stack ss = new Apex.Stack()) in my Dev org. i.e. Such class is available in SF but due to some known limitation not documented. – Sanjay Kharwar Jan 2 '19 at 12:51

The Apex.Stack class has known limitations and bugs, which is why it was never formally released in the documentation. Do not use it if you care about the stability of your code.

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.