Header Ads Widget

Responsive Advertisement

Apex - Basics

 What is Apex?

Apex is a strongly typed, object-oriented programming language developed by Salesforce. It enables developers to execute flow and transaction control statements on the Salesforce Platform server in conjunction with API calls. Apex is designed to be easy to use, yet powerful enough to handle complex business processes.

Use Cases and Benefits of Apex

  • Automating Business Processes: Apex can automate repetitive tasks, reducing manual work and increasing efficiency.
  • Creating Custom Applications: Developers can build sophisticated, custom applications that run natively on the Salesforce Platform.
  • Integrating with External Systems: Apex allows seamless integration with external web services and systems.

Apex vs. Other Programming Languages

  • Syntax Similarity: Apex syntax is similar to Java, making it familiar to developers with a Java background.
  • Built for Salesforce: Apex is specifically designed for the Salesforce environment, providing seamless integration with Salesforce features and functionalities.

 

Apex Syntax and Basics

Data Types and Variables

Apex supports various data types, including primitives, collections, sObjects, and user-defined types.

// Primitive data types

Integer count = 10;

String name = 'Salesforce';

Boolean isActive = true;

Date today = Date.today();

Operators

Operators in Apex include arithmetic, relational, logical, and assignment operators.

Integer a = 10;

Integer b = 5;

Integer sum = a + b; // Arithmetic

Boolean result = a > b; // Relational

Boolean isActive = true && false; // Logical

Integer count = 10; // Assignment

Control Flow Statements

Control flow statements include conditional statements and loops.

// If-else statement

if (a > b) {

    System.debug('a is greater than b');

} else {

    System.debug('a is less than or equal to b');

}

 

// For loop

for (Integer i = 0; i < 5; i++) {

    System.debug('Value of i: ' + i);

}

 

// While loop

Integer j = 0;

while (j < 5) {

    System.debug('Value of j: ' + j);

    j++;

}

Collections

Collections in Apex include Lists, Sets, and Maps.

// List

List<String> fruits = new List<String>{'Apple', 'Banana', 'Mango'};

System.debug(fruits);

 

// Set

Set<Integer> numbers = new Set<Integer>{1, 2, 3, 4, 5};

System.debug(numbers);

 

// Map

Map<String, Integer> scores = new Map<String, Integer>{'John' => 90, 'Jane' => 85};

System.debug(scores);

Exception Handling

Exception handling ensures your code can gracefully handle unexpected errors.

try {

    Integer result = 5 / 0;

} catch (ArithmeticException e) {

    System.debug('Cannot divide by zero');

} finally {

    System.debug('Execution completed');

}

 

Apex Classes and Objects

Defining Classes and Methods

Classes in Apex are blueprints for objects. Methods define the behavior of these objects.

 

public class Calculator {

    public Integer add(Integer a, Integer b) {

        return a + b;

    }

}

Creating and Using Objects

Objects are instances of classes.

Calculator calc = new Calculator();

Integer sum = calc.add(10, 5);

System.debug('Sum: ' + sum);

Class Constructors

Constructors initialize objects.

public class Person {

    public String name;

    public Integer age;

 

    // Constructor

    public Person(String name, Integer age) {

        this.name = name;

        this.age = age;

    }

}

 

// Creating an object using constructor

Person john = new Person('John', 30);

System.debug('Name: ' + john.name + ', Age: ' + john.age);

Access Modifiers

Access modifiers control the visibility of classes, methods, and variables.

  • public: Accessible anywhere.
  • private: Accessible only within the defining class.
  • global: Accessible across namespaces.
  • protected: Accessible within the defining class and its subclasses.

Assignments:

1.   Create variables for storing your name, age, and today's date. Print these values using System.debug().

2.   Define a class Car with attributes makemodel, and year. Create a constructor to initialize these attributes and a method to display the car details. Create an object of the class and display its details.

3.   Perform basic arithmetic operations (addition, subtraction, multiplication, division) on two numbers and print the results.

4.   Write a program that prints the first 10 even numbers using a for loop.

5.   Create a list of your favorite books, a set of unique numbers, and a map of student names and their grades. Print these collections.

6.   Write a program that attempts to convert a string to an integer. Handle the possible NumberFormatException and print an appropriate message.

 

For more in-depth tutorials and learning paths, refer to the following resources:

Happy learning!

Post a Comment

0 Comments