Lesson 2: Advanced Salesforce Development Concepts
Table of contents
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:
public class
defines a reusable class.calculateDiscount
contains logic to compute discounts.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:
Tax_Config__mdt
is a custom metadata object.SELECT Region__c, TaxRate__c
retrieves tax data fields.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:
Database.Batchable
processes bulk data in smaller chunks.start
defines the query scope for data processing.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.