Learn SALESFORCE-SCRIPTING with Real Code Examples
Updated Nov 27, 2025
Code Sample Descriptions
1
Simple Apex Trigger on Account
trigger AccountTrigger on Account (before insert) {
for(Account acc : Trigger.new) {
acc.Custom_Field__c = 'New Account';
}
}
A trigger that updates a custom field when an Account is inserted.
2
Apex Class to Calculate Discount
public class DiscountCalculator {
public static Decimal ApplyDiscount(Decimal price, Decimal percent) {
return price - (price * percent / 100);
}
}
A simple Apex class method that calculates discounted price.
3
Batch Apex to Update Opportunities
global class OpportunityBatch implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator('SELECT Id FROM Opportunity WHERE StageName = 'Prospecting'');
}
global void execute(Database.BatchableContext BC, List<Opportunity> scope) {
for(Opportunity opp : scope) {
opp.StageName = 'Closed Won';
}
update scope;
}
global void finish(Database.BatchableContext BC) {}
}
Batch Apex class to update Opportunity Stage to 'Closed Won'.
4
Trigger to Send Email on Contact Creation
trigger ContactEmailTrigger on Contact (after insert) {
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
for(Contact c : Trigger.new) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {c.Email});
mail.setSubject('Welcome!');
mail.setPlainTextBody('Hello ' + c.FirstName + ', welcome to our platform.');
emails.add(mail);
}
Messaging.sendEmail(emails);
}
Send a welcome email when a new Contact is created.
5
Apex Trigger to Prevent Deletion
trigger PreventAccountDelete on Account (before delete) {
for(Account acc : Trigger.old) {
Integer contactCount = [SELECT COUNT() FROM Contact WHERE AccountId = :acc.Id];
if(contactCount > 100) {
acc.addError('Cannot delete account with more than 100 contacts');
}
}
}
Prevent deletion of Account records with more than 100 related Contacts.
6
Scheduled Apex to Update Leads
global class LeadStatusScheduler implements Schedulable {
global void execute(SchedulableContext sc) {
List<Lead> leads = [SELECT Id FROM Lead WHERE Status != 'Contacted'];
for(Lead l : leads) {
l.Status = 'Contacted';
}
update leads;
}
}
Scheduled Apex to update all Leads' Status to 'Contacted' every night.
7
Apex Trigger to Auto-Assign Owner
trigger CaseOwnerTrigger on Case (before insert) {
Id queueId = [SELECT Id FROM Group WHERE Name = 'Support Queue' AND Type='Queue' LIMIT 1].Id;
for(Case c : Trigger.new) {
c.OwnerId = queueId;
}
}
Automatically assign new Cases to a default queue.
8
Dynamic SOQL in Apex
String objectName = 'Account';
String fieldName = 'Name';
String filterValue = 'Acme';
String query = 'SELECT Id, ' + fieldName + ' FROM ' + objectName + ' WHERE ' + fieldName + ' LIKE '%' + filterValue + '%'';
List<SObject> results = Database.query(query);
Build and execute a dynamic SOQL query based on user input.
9
Apex Trigger to Log Changes
trigger AccountChangeLog on Account (after update) {
List<Account_Log__c> logs = new List<Account_Log__c>();
for(Account acc : Trigger.new) {
Account oldAcc = Trigger.oldMap.get(acc.Id);
if(acc.Name != oldAcc.Name) {
logs.add(new Account_Log__c(Account__c=acc.Id, Old_Name__c=oldAcc.Name, New_Name__c=acc.Name));
}
}
insert logs;
}
Log Account name changes into a custom object.
10
Use Custom Metadata in Apex
List<MySettings__mdt> settings = [SELECT MasterLabel, Threshold__c FROM MySettings__mdt];
for(MySettings__mdt s : settings) {
System.debug('Threshold for ' + s.MasterLabel + ': ' + s.Threshold__c);
}
Read a custom metadata type to control logic in Apex.