The best applications are coded properly. The "properly" means the code not only does its job well, but is also easy to understand, add to, maintain and debug.
Coding standards are great! We want to ensure that the Salesforce org is self documented. The consistent and well-written code is critical to ensuring high quality standards across all applications.
Here you will find the Best Practices (Naming Conventions & Coding Standards) of development components for Salesforce org.
Salesforce Naming Conventions (Point and Click Tools) | ||
---|---|---|
Component | Naming Convention | Example |
Custom Object Name Custom Field Name Record Types Validation Rules Workflow Rules Approval Processes Custom Buttons & Links ...................... |
CapitalizedCamelCase without Underscores | SalesOrder__c |
Salesforce Naming Conventions (Apex) | ||
---|---|---|
Component | Naming Convention | Examples with Suffix |
Apex Trigger | CapitalizedCamelCase without Underscores | AccountTrigger |
Apex Trigger Handler Class | CapitalizedCamelCase without Underscores | AccountTriggerHandler |
Apex Utility Class | CapitalizedCamelCase without Underscores | AccountUtility |
Apex Helper Class | CapitalizedCamelCase without Underscores | AccountRollupHelper |
Apex Wrapper class | CapitalizedCamelCase without Underscores | AccountWrapper |
Apex Batch Class | CapitalizedCamelCase without Underscores | ProcessAccountsBatch |
Apex Schedule Class | CapitalizedCamelCase without Underscores | ProcessAccountsBatchSchedule |
Apex/Visualforce Controller | CapitalizedCamelCase without Underscores | AccountController |
Apex/Visualforce Controller Extension | CapitalizedCamelCase without Underscores | AccountControllerExtension |
Apex Test Data Factory | CapitalizedCamelCase without Underscores | AccountTestDataFactory |
Apex Test Class Name | CapitalizedCamelCase without Underscores | AccountTest |
Apex Class Method | lowerCamelCase without Underscores | public void calculateSales(){} |
Apex Constant Class Variables | CAPITALIZED_WITH_UNDERSCORES | public static final OBJECT_TYPE_ACCOUNT = ‘Account’; |
Boolean Variables | lowerCamelCase without Underscores | isActive, hasErrors, hasLineItems, isSuccess etc. |
Salesforce Naming Conventions (Visualforce / Static Resource / Custom Labels) | ||
---|---|---|
Component | Naming Convention | Examples with Suffix |
Visualforce Page | CapitalizedCamelCase without Underscores | ViewSales |
Static Resources | lowerCamelCase without Underscores | paginationResource |
Custom Label | CapitalizedCamelCase without Underscores | ErrorMessage |
Salesforce Naming Conventions (Lightning) | ||
---|---|---|
Component | Naming Convention | Examples with Suffix |
Lightning Application | CapitalizedCamelCase without Underscores | HRMApp |
Lightning Interface | CapitalizedCamelCase without Underscores | PaginationInterface |
Lightning Component | lowerCamelCase without Underscores | processRecordsComponent |
Lightning Token | lowerCamelCase without Underscores | integrationCredentialsToken |
Lightning Application Event | lowerCamelCase with Underscores | deleteRecord_AppEvent |
Lightning Component Event | lowerCamelCase with Underscores | sendAccountToSAP_CompEvent |
Acceptable Keywords & Access Modifiers (Y) | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Class Type | global | public | private | with sharing | without sharing | @isTest annotation | @testSetup annotation | @TestVisible annotation | @AuraEnabled annotation | ||
Apex Constant Class | Y | Y | |||||||||
Apex Trigger Handler | Y | Y | |||||||||
Apex Utility | Y | Y | Y | ||||||||
Apex Helper | Y | Y | Y | ||||||||
Apex Schedule | Y | Y | |||||||||
Apex Batch | Y | Y | |||||||||
Apex Wrapper | Y | Y | |||||||||
Visualforce Controller | Y | Y | |||||||||
Visualforce Controller Extension | Y | Y | |||||||||
Lightning Controller | Y | Y | Y | ||||||||
Apex Test Data Factory | Y | Y | |||||||||
Apex Test Class | Y | Y | |||||||||
Apex Non-Test Class Private Variables | Y | Y | |||||||||
Apex Non-Test Class Private Methods | Y | Y | |||||||||
Apex Setup / Common Method | Y | Y |
Custom Settings | ||
---|---|---|
Component | Best Practice | Examples |
List Custom Settings in Apex | Don't use SOQL Query to access custom setting. Use following List custom setting methods to access it. (1) getAll() (2) getInstance(dataSetName) (3) getValues(dataSetName) |
(1) Get all records of a custom setting
Map triggerSetting = TriggerSetting_c.getAll();
for(String triggerName : triggerSetting.keySet()){
System.Debug('>>triggerName<<'+triggerName);
System.Debug('>>Trigger flag Activate/Deactivate<<'+triggerSetting.get(triggerName).Deactivate__c);
}
(2) Get specific record from the custom setting using literal
TriggerSetting_c accountTriggerSetting = TriggerSetting_c.getInstance('Account');
System.Debug('>>accountTriggerSetting<<'+accountTriggerSetting);
System.Debug('>>accountTriggerSetting<<'+accountTriggerSetting.Deactivate__c);
(3) Get specific record from the custom setting using literal
TriggerSetting_c accountTriggerSetting = TriggerSetting_c.getValues('Account');
System.Debug('>>accountTriggerSetting<<'+accountTriggerSetting);
System.Debug('>>accountTriggerSetting<<'+accountTriggerSetting.Deactivate__c);
|
Hierarchy Custom Setting in Apex | Don't use SOQL Query to access custom setting. Use following Hierarchy custom setting methods to access it. (1) getInstance() (2) getInstance(userId) (3) getInstance(profileId) (4) getOrgDefaults() (5) getValues(userId) (6) getValues(profileId) |
(1)
TPM_ApplicationSetting_c applicationSetting = TPM_ApplicationSetting_c.getOrgDefaults();
System.Debug('>>applicationSetting<<'+applicationSetting);
System.Debug('>>applicationSetting<<'+applicationSetting.TPM_RecordsPerPage__c);
if( applicationSetting.TPM_RecordsPerPage__c <> null ){
// Your code here...
}
(2)
TPM_ApplicationSetting_c applicationSetting = TPM_ApplicationSetting_c.getInstance();
System.Debug('>>applicationSetting<<'+applicationSetting);
System.Debug('>>applicationSetting<<'+applicationSetting.TPM_RecordsPerPage__c);
if( applicationSetting.TPM_RecordsPerPage__c <> null ){
// Your code here...
}
|
Custom Metadata Types | ||
---|---|---|
Best Practice | Examples | |
Use SOQL query to access Custom Metadata Type record | (1) Access All records
List lstKPIConfigurations = [SELECT MasterLabel, QualifiedApiName, DeveloperName, TPM_Active__c FROM TPM_KPIConfiguration__mdt];
(2) Access specific record
TPM_KPIConfiguration__mdt kpiConfiguration = [SELECT MasterLabel, QualifiedApiName, DeveloperName, TPM_Active__c FROM TPM_KPIConfiguration__mdt WHERE QualifiedApiName = 'WaveAnalytics1'];
|
(a) Context Variable Considerations
(b) Trigger Context Variables
(c) Triggers and Recovered Records for after undelete trigger
(a) Get Started with Apex Unit Tests
(b) @isTest(isParallel=true) annotation
(c) Common Test Utility Classes for Test Data Creation
(d) Standard Test Methods
(a) Hello World Trigger Test Class
(b) An Introduction to Apex Code Test Methods
public PageReference saveAccounts(){
Savepoint sp = Database.setSavepoint();
try{
// Your code logic here...
}
catch(System.Exception ex){
Database.rollback(sp);
// Show error to user
}
return null;
}
(1) Learn Salesforce with Trailhead
(2) Salesforce for Starters (useful for Consultants and Functional folks)
(3) Salesforce for UI/UX Developers (useful for SmartUI and Visualforce developers)
(5) Integration with Salesforce (useful for Apex developers + Integration folks)
(6) A Beginner’s Guide to Object-Oriented Programming with Apex