function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Douglas HauckDouglas Hauck 

Error message: "Maximum number of enum items exceeded"

I am creating a helper class with several static methods to assist with various common tasks.  One of the methods takes a parameter that can have one of about 130 preset string values.  To make it easier for future developers, I created an Enum with an item for each of the preset values.

Unfortunately, I am getting an error message from the Developer Console: "Maximum number of enum items exceeded: 100".  That limit will create a real problem for me, and it also seems kind of arbitrary (as opposed to, say, 255 or 65535).  I was hoping to find somewhere that I could maybe bump that up, but I can't find any mention of it at all, in any of the documentation.

Does anyone know where this comes from, why there is this seemingly arbitrary limit, and whether it can be adjusted?  If not, has anyone run into this before who could maybe help me out with a workaround?  Yes, I know I could make a Map, but Maps don't work the same (especially as regards the nice auto-fill functionality that comes from an Enum class.
Douglas HauckDouglas Hauck
Dunno if there is a way to bump up the actual Enum limit.  My solution was to make my own "Enum" class.

Basically, I created an immutable class, with three readonly ("final" in Apex) fields: name, value, and ordinal.  Also, the class has only a single, private constructor, so the only way to instantiate objects is via a set of static methods, one for each "enum" value I want to provide.

After that, I added the standard Enum methods -- name(), ordinal(), and values() -- as well as one of my own: value(), since my enumeration items can have a different name and value.  I also added/overrode the standard Object methods -- equals(obj), hashCode(), and toString() -- to make sure it plays well with others. 

Cons:
  • It's a lot of work, compared to just banging out an Enum.  I had some help from MS Excel to create the code, though.
  • Might have extra system overhead as well.  (Don't know for sure - depends on how standard Enum is implemented.) 
Pros:
  • No 100 item limit
  • Separate name and value, so value could be just about anything (though I'd recommend sticking to primitives).
  • Works just like an Enum, and shows up better in the auto-complete (Enums always give my a-c problems).
  • Add your own methods as necessary, again, unlike Enums.
  • Best of all (and unlike the Enum) it serializes in and out of JSON without a hitch.
All in all, it's not the perfect solution for every case, but it's another option when you need to get around the Enum limitations. 

Sample code is below.  Feel free to recommend improvements or other solutions.
public class MyEnumClass 
{
    final string itemName;
    final string itemValue;
    final integer itemOrdinal;
    
    private MyEnumClass(string n, string v, integer o)
    {
        itemName = n;
        itemValue = v;
        itemOrdinal = o;
    }
    
    public string name()
    {
        return itemName;
    }
    
    public string value()
    {
        return itemName;
    }
    
    public integer ordinal()
    {
        return itemOrdinal;
    }
    
    public override string toString()
    {
        return 'MyEnumClass{name=' + name() + ', value=' + value() + ', ordinal=' + ordinal() + '}';
    }
    
    public Boolean equals(Object obj) 
    {
        if(obj instanceOf MyEnumClass)	// Note: instanceOf returns false if obj is null
            return 
		((MyEnumClass)obj).itemOrdinal == this.itemOrdinal && 
		((MyEnumClass)obj).itemName == this.itemName && 
		((MyEnumClass)obj).itemValue == this.itemValue;
        return false;
    }
    
    public Integer hashCode()
    {
        return itemOrdinal * itemName.hashCode() * itemValue.hashCode();
    }
    
    public static List<MyEnumClass> values()
    {
        return new List<MyEnumClass>
        { 
            MyEnumClass.Item_1,
            MyEnumClass.Item_2,
            MyEnumClass.Item_3
	};
    }
    
    public static MyEnumClass Item_1 {get { return new MyEnumClass('Item_1', 'Item 1', 0); }} 
    public static MyEnumClass Item_2 {get { return new MyEnumClass('Item_2', 'Item 2', 1); }} 
    public static MyEnumClass Item_3 {get { return new MyEnumClass('Item_3', 'Item 3', 2); }} 
}