SFDC Stop - Always the latest about Salesforce


Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.


Telegram logo   Join our Telegram Channel

Monday, 16 September 2024

Apex Constants and Enums - Salesforce Apex Tutorial Part 14 to 16 | Code Snippet

Aloha Trailblazers,


In this post, I'm sharing the code snippet for Coding with Confidence: The Fun Way to Learn Salesforce Apex tutorial series part 14 to 16:
// * Constants and Enums in Apex

/*
? What is a Constant?
* A constant is a variable whose value cannot be updated after initialization. Constants are defined in apex using the "final" keyword.
*/

final Decimal PI = 3.14;

System.debug('Value of PI Decimal constant is ' + PI);

PI = 2.45;

System.debug('Value of PI Decimal constant is ' + PI);

/*
? What is an Enum?
* An enum (short for enumeration) is a special data type in programming that defines a fixed set of constants. They're a set of possible values that don't otherwise have a numerical order
*/

public enum DaysOfWeek { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }

DaysOfWeek currentDay = DaysOfWeek.SUNDAY;
System.debug('Current Day of Week is ' + currentDay);

// * Enum Methods

// * Getting all values of enum
List<DaysOfWeek> daysOfWeekValues = DaysOfWeek.values();
System.debug('Values of DaysOfWeek enum: ' + daysOfWeekValues);

// * Name of enum as string
String monday = DaysOfWeek.MONDAY.name();
System.debug('Converting MONDAY from enum to string: ' + monday);

// * String to enum
DaysOfWeek mondayEnum = DaysOfWeek.valueOf('tuesday');
System.debug('Converting monday from string to enum: ' + mondayEnum);

// * Ordinal - It'll return the position of current item, in the list of enum values starting from index 0
Integer thursdayIndex = DaysOfWeek.THURSDAY.ordinal();
System.debug('Ordinal of THURSDAY enum: ' + thursdayIndex);

System.debug('Is mondayEnum equal to MONDAY?: ' + DaysOfWeek.MONDAY.equals(mondayEnum));

// * Common example of enum

System.debug(LoggingLevel.NONE, 'Logging level set to NONE in this log');
System.debug(LoggingLevel.ERROR, 'Logging level set to ERROR in this log');
System.debug(LoggingLevel.WARN, 'Logging level set to WARN in this log');
System.debug(LoggingLevel.INFO, 'Logging level set to INFO in this log');
System.debug(LoggingLevel.DEBUG, 'Logging level set to DEBUG in this log');
System.debug(LoggingLevel.FINE, 'Logging level set to FINE in this log');
System.debug(LoggingLevel.FINER, 'Logging level set to FINER in this log');
System.debug(LoggingLevel.FINEST, 'Logging level set to FINEST in this log');

YouTube Tutorials

You can check out the tutorials below as well:

Master Apex Constants: A Quick Guide - Salesforce Apex Tutorial Part 14

Unleash the Power of Enums in Apex! - Salesforce Apex Tutorial Part 15

Elevate Your Apex Logging with Enums: A Must-Watch! ⚡ - Salesforce Apex Tutorial Part 16


Check out the tutorials and let me know your feedback in the comments down below.

Happy Trailblazing!!

No comments:

Post a Comment