Learn SALESFORCE-APEX with Real Code Examples
Updated Nov 27, 2025
Code Sample Descriptions
1
Apex Trigger - Enforce Validation
trigger OpportunityValidation on Opportunity (before update) {
for (Opportunity opp : Trigger.new) {
if (opp.StageName == 'Closed Won' && String.isBlank(opp.Description)) {
opp.addError('Description is required before closing an Opportunity.');
}
}
}
Prevents an Opportunity from being closed without a description.
2
Apex Class - Call Salesforce Internal API (SOQL + DML)
public class AccountUpdater {
public static void normalizeAccounts() {
List<Account> accs = [SELECT Id, Name FROM Account WHERE Name LIKE 'Test%'];
for (Account a : accs) {
a.Name = a.Name.replace('Test', '');
}
update accs;
}
}
Custom Apex class that queries Accounts and updates a field based on logic.