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
BPOORBPOOR 

enum not supporting hyphens?

Hi,

I am trying to create an enum datatype for relationships. Below is the code I am using:

public Enum Enum_RelationShipCode {Father, Mother, Brother, Sister, Uncle, Aunt, Brother-In-Law, Daughter-In-Law, Child}

However, I am getting compile errors when trying to save the class as below.

Expecting '}' but was: '-'
Unexpected Token ','.
Unexpected Token '}'

Can someone help? It appears that Salesforce enums are not supporting hyphens in them.
Narender Singh(Nads)Narender Singh(Nads)
Hi Balaji,
Apex does not support hyphens.

Thanks.
Lucas Duque 9Lucas Duque 9
Hello Balaji,

as far as I know you can't do this in code, cause hyphens cause compilation problems, specifically "Unexpected token".

What you could do is create a array or map of string with these values that have hyphen inside and leave the enum only with others values.

For example:

Using a array would look like this:
List<String> lstRelationShipCode = new List<String>{'Brother-In-Law', '​Daughter-In-Law'};

and so you access the values by the index lstRelationShipCode .get(0) //that return is 'Brother-In-Law'

Using a map would look like this:
Map<String, String> mapRelationShipCode = new Map<String, String>{'Brother-In-Law' => 'Brother-In-Law', '​Daughter-In-Law' => '​Daughter-In-Law'};

and so you access the values by the key mapRelationShipCode.get('Brother-In-Law');

I don't know if this is good for you, but enum with hyphen don't work.

You can also replace hyphen by "_", that would look like this:
public Enum Enum_RelationShipCode {Father, Mother, Brother, Sister, Uncle, Aunt, Brother_In_Law, Daughter_In_Law, Child}

Hope it helps, if yes please mark the answer as the best.

Thks.