Lesson 2: Advanced Salesforce Development Concepts

Lesson 2: Advanced Salesforce Development Concepts


1. Apex Classes

  • Definition: Apex classes encapsulate reusable methods and variables to implement business logic.

  • Use: Define logic for applications.

  • Example: Create a class to calculate account discounts.

public class DiscountCalculator {
    public Decimal calculateDiscount(Decimal revenue) {
        if (revenue > 100000) return revenue * 0.1;
        return revenue * 0.05;
    }
}

Explanation:

  1. public class defines a reusable class.

  2. calculateDiscount contains logic to compute discounts.

  3. if applies conditional calculations for revenue thresholds.


2. Custom Metadata Types

  • Definition: Custom metadata stores configuration and allows dynamic data-driven behavior without changing code.

  • Use: Store application settings.

  • Example: Query metadata for region-specific tax rates.

List<Tax_Config__mdt> taxConfigs = [SELECT Region__c, TaxRate__c FROM Tax_Config__mdt WHERE Region__c = 'North America'];

Explanation:

  1. Tax_Config__mdt is a custom metadata object.

  2. SELECT Region__c, TaxRate__c retrieves tax data fields.

  3. WHERE filters regions to 'North America'.


3. Apex Batch

  • Definition: Apex batch processes large data volumes asynchronously in manageable chunks.

  • Use: Efficient bulk data processing.

  • Example: Batch to update inactive accounts.

global class InactiveAccountsBatch implements Database.Batchable<SObject> {
    global Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('SELECT Id FROM Account WHERE LastActivityDate < LAST_YEAR');
    }
    global void execute(Database.BatchableContext bc, List<SObject> scope) {
        for (Account acc : (List<Account>)scope) {
            acc.Status__c = 'Inactive';
        }
        update scope;
    }
    global void finish(Database.BatchableContext bc) {}
}

Explanation:

  1. Database.Batchable processes bulk data in smaller chunks.

  2. start defines the query scope for data processing.

  3. execute updates records within each chunk.


Summary

  • Apex Classes: Reusable logic encapsulation.

  • Custom Metadata Types: Store dynamic configuration data.

  • Apex Batch: Efficient asynchronous data processing.