Lesson 1: Salesforce Development Basics

Lesson 1: Introduction to Salesforce Development


1. Salesforce Object Query Language (SOQL)

  • Definition: SOQL retrieves records from Salesforce objects, similar to SQL in databases.

  • Use: Extract specific data efficiently.

  • Example: Fetching accounts with revenue above $1M.

List<Account> accounts = [SELECT Name, AnnualRevenue FROM Account WHERE AnnualRevenue > 1000000];

Explanation:

  1. List<Account> stores multiple Account objects.

  2. SELECT Name, AnnualRevenue defines fields to retrieve.

  3. WHERE filters accounts with AnnualRevenue > 1000000.


2. Data Manipulation Language (DML)

  • Definition: DML modifies Salesforce data, including inserting, updating, or deleting records.

  • Use: Perform CRUD operations programmatically.

  • Example: Inserting a new Contact record.

Contact newContact = new Contact(FirstName = 'John', LastName = 'Doe', Email = 'john.doe@example.com');
insert newContact;

Explanation:

  1. new Contact() creates a Contact instance.

  2. FirstName, LastName, Email set field values.

  3. insert commits the new record to the database.


3. Apex Triggers

  • Definition: Triggers automate actions when specific events occur (e.g., insert, update).

  • Use: Enforce rules or sync data.

  • Example: Trigger to update Account fields after Contact insertion.

trigger UpdateAccountTrigger on Contact (after insert) {
    for (Contact con : Trigger.new) {
        Account acc = [SELECT Id, LastContactDate FROM Account WHERE Id = :con.AccountId LIMIT 1];
        acc.LastContactDate = System.today();
        update acc;
    }
}

Explanation:

  1. after insert runs trigger post-insertion.

  2. Trigger.new contains inserted records.

  3. update acc saves updated Account.


Summary

  • SOQL: Query data with filters.

  • DML: Modify database records.

  • Apex Triggers: Automate Salesforce logic.