Apex exercise
### Beginner's Guide to Creating Your First Apex Class
Welcome to your first step into the world of Apex! In this tutorial, we'll guide you through creating a simple Apex class. Apex is a strongly-typed, object-oriented programming language used in Salesforce, and it's similar to Java. Don't worry if you're new to coding—we'll keep it straightforward.
#### Step 1: Access Salesforce Developer Console
1. **Log in to Salesforce**: Use your Salesforce credentials to log in to your Salesforce instance.
2. **Open Developer Console**: Click on the gear icon (⚙️) in the upper right corner of Salesforce and select **Developer Console**.
#### Step 2: Create a New Apex Class
1. **Open the Apex Class creation window**:
- In the Developer Console, go to the top menu and click on **File** > **New** > **Apex Class**.
2. **Name your class**:
- A prompt will appear asking you to name your class. Enter a simple name like `HelloWorld` and click **OK**.
#### Step 3: Write Your First Apex Class
Now that you have your class file open, it's time to write some Apex code.
1. **Understand the basic structure**:
- Apex classes typically start with the keyword `public` or `private`, followed by the class name and a set of curly braces `{}` that contain the body of the class.
2. **Write the code**:
- Replace the default code in your class with the following:
```
apex
public class HelloWorld {
// This is a simple method that prints "Hello, World!" to the debug log
public static void sayHello() {
System.debug('Hello, World!');
}
}
```
Here's what this code does:
- `public class HelloWorld`: Declares a public class named `HelloWorld`.
- `public static void sayHello()`: Declares a method named `sayHello` that doesn’t return any value (`void`). The `static` keyword means you can call this method without creating an instance of the class.
- `System.debug('Hello, World!');`: This line prints "Hello, World!" to the debug log.
3. **Save your class**:
- Press `Ctrl + S` (Windows) or `Cmd + S` (Mac) to save your class.
#### Step 4: Test Your Apex Class
Now that your class is created, you can run it to see the result.
1. **Execute the method**:
- In the Developer Console, click on **Debug** > **Open Execute Anonymous Window**.
2. **Call your method**:
- In the window that opens, type the following code:
```
apex
HelloWorld.sayHello();
```
3. **Execute the code**:
- Click **Execute**. The code will run, and you should see the output
Comments
Post a Comment