Learn Salesforce-scripting - 10 Code Examples & CST Typing Practice Test
Salesforce Scripting refers to the use of programming and declarative scripting tools within Salesforce to automate processes, customize behavior, and extend the platform using Apex, Visualforce, Lightning Web Components, and Flow.
View all 10 Salesforce-scripting code examples →
Learn SALESFORCE-SCRIPTING with Real Code Examples
Updated Nov 27, 2025
Code Sample Descriptions
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.
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.
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'.
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.
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.
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.
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.
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.
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.
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.
Frequently Asked Questions about Salesforce-scripting
What is Salesforce-scripting?
Salesforce Scripting refers to the use of programming and declarative scripting tools within Salesforce to automate processes, customize behavior, and extend the platform using Apex, Visualforce, Lightning Web Components, and Flow.
What are the primary use cases for Salesforce-scripting?
Custom triggers and business logic via Apex. Automating repetitive tasks using Flows and Process Builder. Extending Salesforce UI using LWC or Visualforce. Data validation and integration with external systems. Building custom objects, workflows, and reports
What are the strengths of Salesforce-scripting?
Deep Salesforce ecosystem integration. Supports both declarative and programmatic approaches. Robust security and sharing model enforcement. Real-time automation with triggers and workflows. Strong community and extensive documentation
What are the limitations of Salesforce-scripting?
Governor limits constrain resource usage. Steep learning curve for Apex and advanced LWC. Testing and deployment require sandbox or scratch orgs. Dependent on Salesforce platform releases. Heavy customization can impact maintainability
How can I practice Salesforce-scripting typing speed?
CodeSpeedTest offers 10+ real Salesforce-scripting code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.