Apex Trigger - Enforce Validation - Salesforce-apex Typing CST Test
Loading…
Apex Trigger - Enforce Validation — Salesforce-apex Code
Prevents an Opportunity from being closed without a description.
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.');
}
}
}Salesforce-apex Language Guide
Salesforce Apex is a strongly typed, Java-like programming language used to implement custom business logic on the Salesforce Platform. It provides server-side execution within the multi-tenant Salesforce environment, enabling automation, integrations, transactional operations, and advanced customization beyond declarative tools.
Primary Use Cases
- ▸Trigger-based automation for complex business rules
- ▸Custom REST/SOAP services for integrations
- ▸Batch and async processing for high-volume data jobs
- ▸Custom Lightning Web Component (LWC) backend controllers
- ▸Transactional orchestration and advanced validation logic
Notable Features
- ▸Java-like syntax with Salesforce-specific data types and SObjects
- ▸Trigger framework support for scalable event handling
- ▸Built-in test framework with mandatory code coverage
- ▸Async processing: Queueable, Batchable, Future, Scheduled
- ▸First-class integration support (REST, SOAP callouts)
Origin & Creator
Created by Salesforce to allow controlled, secure server-side programming on the CRM platform, evolving since 2006 alongside the Force.com ecosystem.
Industrial Note
Large enterprises rely heavily on Apex to implement governed workflows, ERP-like processes, and integrations across distributed systems.