Creating Apex Classes

Apex classes are the building blocks for custom solutions on the Salesforce platform. An Apex class is a blueprint for an object and can contain variables, methods, and logic to manipulate data and perform actions.

To create an Apex class, you’ll need to access the Salesforce platform and follow these steps:

   1.Navigate to the Developer Console: This can be done from the setup menu in your Salesforce org.

   2.Click the “New” button: This will open a new tab in the Developer Console where you can create a new Apex class.

   3.Enter a name for the class: Choose a descriptive name for the class that accurately reflects its purpose.

   4.Define the class structure: Apex classes have a specific syntax and structure that includes the class keyword, the class name, and a set of curly braces ({}) to define the class body.

   5.Define variables and methods: Variables store data and methods perform actions. Variables can be defined within the class body using the syntax dataType variableName;.

  6. Methods can be defined using the syntax returnType methodName() { … }

Ex.1

public class AccountHandler {
    public void updateAccount(Id accountId, String newName) {
        Account account = [SELECT Id, Name FROM Account WHERE Id = :accountId];
        account.Name = newName;
        update account;
    }
}

In this example, we’ve defined an Apex class AccountHandler with a single method updateAccount.

The method takes in two parameters – accountId (of type Id) and newName (of type String) – and updates the name of an Account record in the Salesforce database.

The method uses a SOQL query to retrieve the Account record and then updates it using the update statement.

Execution:

————

To execute an Apex class on the Salesforce platform, you’ll need to access the Developer Console and follow these steps:

    1.Open the Developer Console: This can be done from the setup menu in your Salesforce org.

    2.Open the Apex class: In the Developer Console, find the Apex class you want to execute and open it.

    3.Create an anonymous block: An anonymous block is a section of code that can be executed outside of a class or method. To create an anonymous block, click on the “Execute” button in the Developer Console and enter the following code:

AccountHandler acc = new AccountHandler();
acc.updateAccount('001XXXXXXXXXXXXX', 'New Account Name');

    4.Execute the code: Click on the “Execute” button to run the anonymous block.

    5.Verify the results: Check the database to verify that the Account record has been updated with the new name. You can also use the Debug Logs in the Developer Console to check for any error messages or exceptions.

Note: In this example, you’ll need to replace ‘001XXXXXXXXXXXXX’ with the actual ID of an Account record in your Salesforce org.

Ex.2

public class ContactManager {
    public static List getContactsByCity(String city) {
        return [SELECT Id, FirstName, LastName, Email FROM Contact 
        					WHERE MailingCity = :city];
    }
}

Execution:

————

List seattleContacts = ContactManager.getContactsByCity('Seattle');

Ex.3

public class Account {
    public String name;
    public String industry;
    public Integer numberOfEmployees;

    public void setName(String accountName) {
        name = accountName;
    }

    public String getName() {
        return name;
    }
}

To create an object from this class, you can use the new operator, like this:

Account acct = new Account();
acct.setName('Acme Inc.');
acct.industry = 'Technology';
acct.numberOfEmployees = 500;

Leave a Reply

Your email address will not be published. Required fields are marked *