# Getting Started with APEX (Salesforce Development)

# Frontend

As we know Salesforce has **Classic** and **Lightning UI.**

If you want to build any of this UI by coding then salesforce offer’s some technologies such as

* **Classic UI** can be developed using → **Visualforce**
    
* **Lightning UI** can be developed using → **Aura, Lightning Web Component (LWC)**
    

LWC was introduced in 2018 and it is much faster and it is based on latest web-standard.

# Backend

* For backend **Apex language** is used. With the help of apex language you can do various things.
    
    * Read/Update/Delete/Create Data and much more.
        

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1730539188294/2bf71c97-b52a-4358-a786-10d36fcf12e9.png align="center")

* Salesforce provides it’s own code editor to edit or add class.
    
* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1730539385481/2cecf91c-c114-4b8a-b9d0-29ab16ab4314.png align="center")
    
    This is Developer console, here we can create class.
    

# Getting Started with APEX

Let’s write hello world program in Apex

* System.debug(‘Hello world‘)';
    
    * Here **System** is class and **debug()** is method
        

### Concepts related to Heap in Apex

* In Salesforce, the "Apex heap" is related to memory allocation and usage within the Salesforce platform. When Apex code is executed, the system allocates a specific amount of memory, known as the heap, to store variables, objects, and other data. This memory is limited, and Salesforce enforces limits on heap size to prevent inefficient code from consuming too much memory and impacting system performance.
    

**Heap Size Limit**

* **Synchronous Apex**: 6 MB
    
* **Asynchronous Apex** (such as Batch Apex, Queueable Apex, or Scheduled Apex): 12 MB
    

### Primitive Data Types in Salesforce

In Salesforce, primitive data types are the basic data types used to store simple values in Apex.

* **Integer**
    
    * Represents a 32-bit number. Can hold whole numbers from -2,147,483,648 to 2,147,483,647.
        
    * Example: `Integer count = 100;`
        
* **Long**
    
    * Represents a 64-bit number, useful for larger integers.
        
    * Example: `Long largeCount = 9876543210L;`
        
* **Double**
    
    * Represents a 64-bit floating-point number for decimal values.
        
    * Example: `Double price = 19.99;`
        
* **Decimal**
    
    * Used for currency values or precise decimal calculations. Suitable for financial applications where precision is crucial.
        
    * Example: `Decimal amount = 1234.56;`
        
* **String**
    
    * Stores a sequence of characters (text).
        
    * Example: `String name = 'Salesforce';`
        
* **Boolean**
    
    * Represents a binary value of `true` or `false`.
        
    * Example: `Boolean isActive = true;`
        
* **Date**
    
    * Represents a date without a time.
        
    * Example: `Date today =` [`Date.today`](http://Date.today)`();`
        
* **Time**
    
    * Represents a specific time of day without a date.
        
    * Example: `Time now =` [`Time.now`](http://Time.now)`();`
        
* **Datetime**
    
    * Represents a specific date and time.
        
    * Example: `Datetime dt =` [`Datetime.now`](http://Datetime.now)`();`
        
* **ID**
    
    * A 15- or 18-character identifier, commonly used to store Salesforce record IDs.
        
    * Example: `Id recordId = '001D000000IqhSL';`
        
* **Blob**
    
    * Used to store binary data, such as files or attachments. Often encoded to and from strings.
        
    * Example: `Blob fileData = Blob.valueOf('Some data');`
        
* **Object**
    
    * A generic data type that can hold any object, including both custom and standard objects.
        
    * Example: `Object obj = new Account();`
        

**Primitive and Non-Primitive Data type**

* **Primitive Data Types**: Basic data types that store simple values. They are predefined by the language and are used to represent single values like integers, decimals, and booleans.
    
* **Non-Primitive Data Types**: More complex data structures or objects that can store multiple values and often reference other data. These include classes, arrays, collections (like Lists, Sets, and Maps), and custom Apex objects.
    

| Feature | Primitive Data Type | Non-Primitive Data Type |
| --- | --- | --- |
| **Memory Allocation** | Stack (direct values) | Heap (references to data) |
| **Mutability** | Immutable | Mutable |
| **Pass by** | Value | Reference |
| **Default Values** | Specific default values | `null` |
| **Examples in Salesforce** | `Integer`, `Boolean`, `String`, `ID` | `List`, `Set`, `Map`, Custom Class |

# List in Apex

* In Apex, a **List** is a non-primitive data type that stores an ordered collection of elements, which can be of any data type (primitive, sObject, user-defined, or even other Lists). Lists in Apex are similar to arrays in other programming languages, allowing you to store multiple elements and access them by their index.
    
* **Key Characteristics of Lists in Apex:**
    
    1. **Ordered Collection**: Lists maintain the order of elements based on their insertion.
        
    2. **Index-Based Access**: Elements can be accessed, added, or removed based on their index.
        
    3. **Dynamic Size**: Lists in Apex can automatically grow or shrink as elements are added or removed.
        

```plaintext
// Declaring a List of Integers
List<Integer> numbers = new List<Integer>();

// Declaring a List of Strings with initial values
List<String> names = new List<String>{'Alice', 'Bob', 'Charlie'};

// Declaring a List of Accounts
List<Account> accounts = new List<Account>();
```

# Set in Apex

In Apex, a **Set** is a collection type that stores unique, unordered elements. Unlike Lists, Sets do not allow duplicate values and do not maintain the order of elements. Sets are particularly useful when you need to store a collection of unique values or perform operations like membership checks and intersections.

### Key Characteristics of Sets in Apex

1. **Unique Elements**: Sets automatically prevent duplicate values.
    
2. **Unordered**: Sets do not maintain the order of elements as they are added.
    
3. **Dynamic Size**: The size of a Set can grow or shrink as elements are added or removed.
    

```plaintext
Declaring a Set of Strings
Set<String> colors = new Set<String>();

// Declaring a Set of Integers with initial values
Set<Integer> numbers = new Set<Integer>{1, 2, 3, 4, 5};

// Declaring a Set of Accounts
Set<Account> accounts = new Set<Account>();
```

# Map in Apex

In Apex, a **Map** is a collection type that stores key-value pairs, where each key is unique and is associated with a single value. Maps are extremely useful for storing data that requires quick lookup and retrieval based on a specific key. Maps in Apex can store keys and values of any data type, including both primitive types and complex objects.

### Key Characteristics of Maps in Apex

1. **Key-Value Pairs**: Each element in a Map has a unique key and a value associated with that key.
    
2. **Unique Keys**: A Map cannot have duplicate keys. If you add an entry with an existing key, the existing value will be overwritten.
    
3. **Dynamic Size**: Maps can grow or shrink in size as entries are added or removed.
    

### Declaring and Initializing a Map

You can declare a Map with specific data types for both the key and value, such as `String`, `Integer`, `Account`, or other data types.

```plaintext
// Declare a Map with Integer keys and String values
Map<Integer, String> employeeNames = new Map<Integer, String>();

// Declare a Map with String keys and Account values
Map<String, Account> accountMap = new Map<String, Account>();

// Initialize a Map with initial key-value pairs
Map<String, String> countries = new Map<String, String>{
    'US' => 'United States',
    'CA' => 'Canada',
    'MX' => 'Mexico'
};
```

### Common Map Methods

Apex provides various methods to work with Maps. Here are some commonly used ones:

1. **put(key, value)**: Adds a new key-value pair to the Map. If the key already exists, it updates the value.
    
    ```plaintext
    employeeNames.put(101, 'Alice');
    ```
    
2. **putAll(map)**: Adds all key-value pairs from another Map to the current Map.
    
    ```plaintext
    Map<Integer, String> moreEmployees = new Map<Integer, String>{
        102 => 'Bob',
        103 => 'Charlie'
    };
    employeeNames.putAll(moreEmployees);
    ```
    
3. **get(key)**: Retrieves the value associated with a specific key.
    
    ```plaintext
    String employee = employeeNames.get(101); // Returns 'Alice'
    ```
    
4. **remove(key)**: Removes the key-value pair for a specific key.
    
    ```plaintext
    employeeNames.remove(101);
    ```
    
5. **containsKey(key)**: Checks if the Map contains a specific key.
    
    ```plaintext
    Boolean hasEmployee = employeeNames.containsKey(102); // Returns true if key 102 exists
    ```
    
6. **keySet()**: Returns a set of all keys in the Map.
    
    ```plaintext
    Set<Integer> keys = employeeNames.keySet();
    ```
    
7. **values()**: Returns a list of all values in the Map.
    
    ```plaintext
    List<String> values = employeeNames.values();
    ```
    
8. **size()**: Returns the number of key-value pairs in the Map.
    
    ```plaintext
    Integer mapSize = employeeNames.size();
    ```
    
9. **clear()**: Removes all key-value pairs from the Map.
    
    ```plaintext
    employeeNames.clear();
    ```
    
10. **clone()**: Creates a shallow copy of the Map.
    
    ```plaintext
    Map<Integer, String> employeeNamesCopy = employeeNames.clone();
    ```
    

# Constant Variable in Apex:

With the help of **final** keyword you can declare variable as constant, then it’s value will not change.

```plaintext
final Integer salary;
```
