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
Saroja MuruganSaroja Murugan 

Class Error

Hi All,

Hope everybody doing well,

I have studied about enum datatype and trying to understand it by using the below code.

But it throw me an error while saving the apex class MyExample.

Please let me know how to resolve this?

Season:
public enum Season {
WINTER,SPRING,SUMMER,FALL
}

Month:
public enum Month {
JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC
}

MyExample:

public class MyExample {
public List seasonMonths(Season s)
{
List listMonths = new List();
if(s==season.WINTER)
{
listMonths.add(Month.DEC);
listMonths.add(Month.JAN);
listMonths.add(Month.FEB);
}
else if(s==season.SPRING)
{
listMonths.add(Month.MAR);
listMonths.add(Month.APR);
listMonths.add(Month.MAY);
}
else if(s==season.SUMMER)
{
listMonths.add(Month.JUN);
listMonths.add(Month.JUL);
listMonths.add(Month.AUG);
}
else if(s==season.FALL)
{
listMonths.add(Month.SEP);
listMonths.add(Month.OCT);
listMonths.add(Month.NOV);
}
return listMonths;
}
}

Thanks,
Best Answer chosen by Saroja Murugan
Dev_AryaDev_Arya
Hi Saroja,

you need to define the type of list, following code may help you:
public with sharing class MyExample {
    public enum Season {
        WINTER,SPRING,SUMMER,FALL
    }

    public enum Month {
        JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC
    }

    public List<Month> seasonMonths(Season s)
    {
        List<Month> listMonths = new List<Month>();
        if(s==season.WINTER)
        {
            listMonths.add(Month.DEC);
            listMonths.add(Month.JAN);
            listMonths.add(Month.FEB);
        }
        else if(s==season.SPRING)
        {
            listMonths.add(Month.MAR);
            listMonths.add(Month.APR);
            listMonths.add(Month.MAY);
        }
        else if(s==season.SUMMER)
        {
            listMonths.add(Month.JUN);
            listMonths.add(Month.JUL);
            listMonths.add(Month.AUG);
        }
        else if(s==season.FALL)
        {
            listMonths.add(Month.SEP);
            listMonths.add(Month.OCT);
            listMonths.add(Month.NOV);
        }
        return listMonths;
    }
}

Cheers,
Dev​

All Answers

marktukmarktuk
You need to use List<Month> as the return type of the method, as it's a list of months that you are creating.
Dev_AryaDev_Arya
Hi Saroja,

you need to define the type of list, following code may help you:
public with sharing class MyExample {
    public enum Season {
        WINTER,SPRING,SUMMER,FALL
    }

    public enum Month {
        JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC
    }

    public List<Month> seasonMonths(Season s)
    {
        List<Month> listMonths = new List<Month>();
        if(s==season.WINTER)
        {
            listMonths.add(Month.DEC);
            listMonths.add(Month.JAN);
            listMonths.add(Month.FEB);
        }
        else if(s==season.SPRING)
        {
            listMonths.add(Month.MAR);
            listMonths.add(Month.APR);
            listMonths.add(Month.MAY);
        }
        else if(s==season.SUMMER)
        {
            listMonths.add(Month.JUN);
            listMonths.add(Month.JUL);
            listMonths.add(Month.AUG);
        }
        else if(s==season.FALL)
        {
            listMonths.add(Month.SEP);
            listMonths.add(Month.OCT);
            listMonths.add(Month.NOV);
        }
        return listMonths;
    }
}

Cheers,
Dev​
This was selected as the best answer
Saroja MuruganSaroja Murugan
Thanks for your help!!

The class saved but how to execute this code in Developer Console?

I tried like below

System.debug(Season.WINTER);
MyExample obj = new MyExample();
System.debug(obj.seasonMonths(Season.WINTER));
marktukmarktuk
If you've saved the example posted by dev_arya then the enums are now within the MyExample class. Your code would need updating to:
System.debug(MyExample.Season.WINTER);
MyExample obj = new MyExample();
System.debug(obj.seasonMonths(MyExample.Season.WINTER));
I'm not sure how well the debug log would handle a list of enums. It might be better to loop through them and debug out the string values using the name() method.
MyExample obj = new MyExample();
List<MyExample.Month> months = obj.seasonMonths(MyExample.Season.WINTER);

for (MyExample.Month m : months)
{
    System.debug(m.name());
}


 
Saroja MuruganSaroja Murugan
Thanks dev_arya and Marktuk it's working great!!

Thank you so much for all your help.
Dev_AryaDev_Arya
You need to mention the class name. Season is not the class, u need to refer it through its parent. Try this in your anonymous apex:
System.debug('****Season WInter value: ' + MyExample.Season.WINTER);
MyExample myExampleObj = new MyExample();
System.debug('****Result: ' + myExampleObj.seasonMonths(MyExample.Season.WINTER));

In the logs, you can find something similar to these lines:
12:02:41.14 (16749085)|USER_DEBUG|[2]|DEBUG|****Season WInter value: WINTER
12:02:41.14 (17889940)|USER_DEBUG|[4]|DEBUG|****Result: (DEC, JAN, FEB)
if you need reference to how to anaylze logs, please refer this:
https://trailhead.salesforce.com/trails/force_com_dev_beginner/modules/developer_console/units/developer_console_logs

Cheers,
Dev