What are Data Types in Apex in 2023? : SF

What are Data Types in Apex in 2023?
by: SF
blow post content copied from  Forcetalks
click here to view original post


What is a Data Type in Salesforce Apex?

In Salesforce Apex, a data type is like a category or a label that tells the computer what kind of information is stored in a variable (a storage box for information). It helps the computer understand how to handle and work with that information.

How Many Types of Data Are There in Salesforce Apex?

There are different types of data in Salesforce Apex, and they can be grouped into two main categories:

1. Data Types: These are the basic building blocks.

Below mentioned are the data types in Salesforce Apex:

  • Blob: Represents binary data.

Blob myBlob = Blob.valueOf('Hello, World!');

  • Boolean: Represents true or false values.

Boolean isTrue = true;

  • Date: Represents a date without a time component.

Date myDate = Date.today();

  • DateTime: Represents a date and time.

Datetime myDatetime = Datetime.now();

  • Decimal: Represents a number with decimal places.

Decimal myDecimal = 42.2;

  • Double: Represents a 64-bit double-precision number.

Double myDouble = 42.2;

  • ID: Represents a record ID.

ID recordId = '001XXXXXXXXXXXXXXX';

  • Integer: Represents whole numbers.

Integer myInt = 42;

  • Long: Represents large whole numbers.

Long myLong = 4200000000;

  • String: Represents a sequence of characters.

String myString = 'Hello, World!';

  • Time: Represents a specific time of day.

Time MyTime = Time.newInstance(12, 30, 0, 0);

dont miss out iconDon't forget to check out: Custom Metadata Types: Your Recipe for API Key Security

2. Collections in Salesforce: A collection in Salesforce Apex is like a basket where you can put many things together. It's a special box that allows you to keep multiple pieces of information in one place. There are three main types:

  • List: A list is like a shopping list where you can write down and keep many items in order.
    • Order Matters: In a list, the order of items is important. Each item has a specific position, called an index, and you can access items using their index.
    • Use When Order Matters: Use a list when the order of items is meaningful, like a sequence of steps or a timeline.

Example: Adding and Accessing:

List<String> fruits = new List<String>{'Apple', 'Orange', 'Banana'}; fruits.add('Grapes');
// Adds 'Grapes' to the end of the list
String myFruit = fruits[1];
// Accesses the second item ('Orange') using the index
  • Set: A set is like a collection of unique treasures. It only keeps one of each kind.
    • No Duplicates: Sets don't allow duplicate values. If you try to add the same item twice, it only keeps one.
    • Use When You Need Uniqueness: Use a set when you want to make sure each item is unique, like keeping track of distinct values.

Example: Ensuring Uniqueness:

Set<Integer> uniqueNumbers = new Set<Integer>{1, 2, 3, 3, 4};
// The set will only have 1, 2, 3, and 4, eliminating the duplicate '3'.
  • Map: A map is like a dictionary where you match one thing with another. For example, you match a name with a phone number.
  • Key-Value Pairs: Maps store information as key-value pairs. Each key is unique, and it maps to a specific value.
  • Use for Associations: Use a map when you want to associate one piece of information with another, like matching names with ages. Collections help keep your information organized, like putting all your toys in one box or all your books on one shelf. They make it easier for your computer program to work with lots of data at once
  • Example: Associating Information:
Map<String, Integer> ages = new Map<String, Integer>{'Alice' => 25, 'Bob' => 30, 'Charlie' => 22};
Integer bobAge = ages.get('Bob'); 
// Retrieves the age of 'Bob'.
  • sObject Data Type: In Salesforce, a sObject is like a special container that holds data for a specific type of object, such as an Account, Contact, or Custom Object.

Example - Creating a sObject

Account myAccount = new Account(Name='My Company', Industry='Technology');

Here, myAccount is an instance of the Account sObject, holding information like the company name and industry.

  • Fields and Properties: A sObject has fields, which are like slots where you can store specific types of information (e.g., Name, Industry).

You access and modify these fields using dot notation.

  • Use for Database Operations: sObjects are often used when interacting with the Salesforce database. You can create, update, and query records using sObjects.

Example

// Creating a new Account record and inserting it into the database.
Account newAccount = new Account(Name='New Company', Industry='Finance');
insert newAccount;
  • Custom Objects: sObjects aren't just limited to standard objects like Account; they can also represent custom objects you create in Salesforce.

Example

// Creating an instance of a custom object named 'Custom_Object__c'.
Custom_Object__c customRecord = new
Custom_Object__c(Name='Custom Record');

In simple terms, think of a sObject as a special box that holds all the details about a specific type of thing in Salesforce, whether it's a standard object provided by Salesforce or a custom object you've designed. It helps you work with and manipulate data related to that specific type of object.

  • Enum Data Type: An Enum, short for Enumeration, is a special data type in Salesforce Apex that defines a set of named constant values.

Defining an Enum:

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

Here, DayOfWeek is an Enum that represents the days of the week as constant values.

Using Enum Values:

You can use Enum values to represent specific, predefined options in your code using Enum Values:

DayOfWeek today = DayOfWeek.WEDNESDAY;

Now, today holds the constant value representing Wednesday.

dont miss out iconCheck out another amazing blog by Bhawana here: What are Apex Basics? A Guide in 2023

Conclusion

In simple terms, an Enum in Salesforce Apex is like a list of predefined options that you can use in your code. It helps make your code more readable and less error-prone by providing clear, named values for specific scenarios. Enums are especially useful when you want to represent a set of constant values that belong together.

The post What are Data Types in Apex in 2023? appeared first on Forcetalks.


December 08, 2023 at 12:26AM
Click here for more details...

=============================
The original post is available in Forcetalks by SF
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Salesforce