Download presentation
Presentation is loading. Please wait.
1
Salesforce Development
Best Practices
2
Salesforce Development Approach
Agile Always Salesforce exists as Agile Environment Pre-built objects, fields, GUI Tools for configuration/workflow without coding Perfect for Iterations – “Make work, Make Better”
3
Salesforce Development Approach
Agile Moves Quickly Fast pace, messy place Organization helps to keep cleaner Need to know where to go Standards guide where logic is located
4
Salesforce Development Tools
Depends on team sizes and personal preferences Salesforce Direct Online – Should be avoided with exceptions Salesforce Developer Console Better alternative to Direct Online Has performance issues with large files Good debugging tools, even if other Dev Environments are used Salesforce Debug option works for Community Users Eclipse Favorite to most IT geeks due to familiarity Sublime Text 3/Mavensmate My preferred environment
5
Salesforce Development – Source Control
Salesforce Has No Built In Source Control Yet…. Eclipse with SVN Publishing tools with File Compare File history Sublime Text 3/Mavensmate Server Compare upon saving No File History; Can undo all changes on local copy after saving
6
Salesforce Development Areas
Validations Workflows Standard Flow Process Builder Triggers Visual Force Pages Visual Force Components Apex Classes
7
Salesforce Development - Validations
Naming Convention Use <Field Name> or <Business Process> first followed by subject (i.e. Account Number Length Check, Category Choice of Other Requires Text, etc.) Descriptions – Full length description of the validation Error Messages Field – Should be first choice to assist user with correction Top of Page – Be overly descriptive and use Field Labels when applicable
8
Salesforce Development - Workflows
Process Builder Preferred over others once GA in Spring ‘15 More organized and easier to follow holistically Can contain more decision making Flow Similar to Process Builder Best when in need of user interaction with process Can also replace Apex Trigger, if no coding is preferred Standard Start to migrate to Process Builder after Spring ’15 release
9
Salesforce Development - Triggers
Apex Code Suggested to Use Template for all object Triggers See Example Next Slide Contains minimal logic Calls <Object>TriggerHandler Class to do decision flow Handles/Prevents Recursion Calls other procedures for business processing to keep organized (know where to go)
10
Salesforce Development – Example Trigger
trigger ApplicationTrigger on Application__c (before insert, before update, before delete, after insert, after update, after delete, after undelete) { ApplicationTriggerHandler handler = new ApplicationTriggerHandler(Trigger.isExecuting, Trigger.size); /* Before Insert */ if(Trigger.isInsert && Trigger.isBefore){ handler.OnBeforeInsert(Trigger.new); } /* Before Update */ else if(Trigger.isUpdate && Trigger.isBefore){ handler.onBeforeUpdate(Trigger.old, Trigger.new, Trigger.newMap, Trigger.oldMap); …
11
Salesforce Development – Example Trigger Handler
public class ApplicationTriggerHandler{ public static boolean onBeforeInsert_FirstRun = true; public void onBeforeInsert(List<Application__c> newObjects) { if(onBeforeInsert_FirstRun) { addTeamToApplications(newObjects); addProgramTranslations(newObjects, null); CountryLookupServices.setCountryLookup(newObjects, null); // PREVENT RECURSION onBeforeInsert_FirstRun = false; }
12
Salesforce Development – VF Pages
Naming Convention If multiple applications within same organization, first prefix could be three letter acronym for the application (i.e. PPA_ for Promotion Planning Application) Add Prefix related to Application Area (i.e. ADM for Administration Area: PPA_ADM_Main_Menu, PPA_ADM_User_Management, etc.) Extensions Best if named after page (i.e. PPA_ADM_Main_Menu_Ext, PPA_ADM_User_Management_Ext, etc.) Comments Add Standard Company Template to all Pages with Name, Description, Author, Modification Log, etc. Use throughout Code to Describe Areas of Page (lack of WYSIWYG)
13
Salesforce Development – VF Components
Naming Convention Similar to VF Pages Comments Add Standard Company Template to all Pages with Name, Description, Author, Modification Log, etc. Use throughout Code to Describe Areas of Component (lack of WYSIWYG)
14
Salesforce Development – Apex Classes
Comments, Comments, Comments Organize – Use MVC approach MODEL <Object>Model Apex-Lang: an open-source library of helper classes written purely in apex whose goal is to address shortcomings in the core apex classes Richard Vanhook’s Base Model: Logic for pulling data Easier to adjust with Agile approach when object changes happen frequently VIEW Visual Force Pages / Components CONTROLLER Page Extensions <Object>Services Show Example Business Logic for Object
15
Salesforce Development – Apex Classes Models
Basic Example Given this SOQL query: SELECT name FROM account WHERE employees < 10 Here is the corresponding code to create the query string via a SoqlBuilder: new al.SoqlBuilder() .selectx('name') .fromx('account') .wherex(new al.FieldCondition('employees').lessThan(10)) .toSoql(); - See more at:
16
Salesforce Development – Apex Classes AccountModel Example
public with sharing class AccountModel extends BaseModel { public AccountModel() { super('Account'); } public AccountModel(sObject obj) { super('Account', obj); protected override List<BaseModel> castToModelObjects(List<sObject> objectList) { List<AccountModel> modelList = new List<AccountModel>(); for(sObject obj : objectList) { modelList.add(new AccountModel( obj )); return (List<BaseModel>)modelList;
17
Salesforce Development – Apex Classes AccountModel Example
protected override void setSelect(al.SoqlBuilder builder) { Set<String> fieldSet = this.getAllFields(); builder.selectx(fieldSet); } public List<AccountModel> getAllRecords() { this.buildQuery(); List<sObject> objectList = this.query(this.builder); return (List<AccountModel>)this.castToModelObjects(objectList);
18
New in Spring ’15 – Test setup method
Set Up Test Data for an Entire Test Class Create test records once and then access them in every test method in the test class Save time when you need to create reference or prerequisite data for all test methods All records in test setup are rolled back at the end of test class execution Changes to test setup data are rolled back after each test method finishes execution Defined in a test class, take no arguments, and return no value
19
New in Spring ’15 – Test setup method
Test Setup Method Considerations Not available annotation is used John’s Suggestion: Use SeeAllData as a last resort Multiple test setup methods are allowed in a test class, but the order in which they’re executed by the testing framework isn’t guaranteed. John’s Suggestion: Don’t use multiple If a fatal error occurs during the execution of a test setup method, the entire test class fails, and no further tests in the class are executed. If a test setup method calls a non-test method of another class, no code coverage is calculated for the non-test method.
20
Test Setup Method – SF Example
@isTest private class CommonTestSetup static void setup() { // Create common test accounts List<Account> testAccts = new List<Account>(); for(Integer i=0;i<2;i++) { testAccts.add(new Account(Name = 'TestAcct'+i)); } insert testAccts;
21
Test Setup Method – John’s Suggested Example
@isTest private class CommonTestSetup static void setup() { testDataHelper.InsertAccounts(20); } public class testDataHelper { public InsertAccount (int numAccounts) { // Create common test accounts List<Account> testAccts = new List<Account>(); for(Integer i=0;i<numAccounts;i++) { testAccts.add(new Account(Name = 'TestAcct'+i)); insert testAccts;
22
Salesforce best practice tips
Best Practice #1: Bulkify your Code Best Practice #2: Avoid SOQL Queries or DML statements inside FOR Loops Best Practice #3: Bulkify your Helper Methods Best Practice #4: Using Collections, Streamlining Queries, and Efficient For Loops Best Practice #5: Streamlining Multiple Triggers on the Same Object Best Practice #6: Querying Large Data Sets Best Practice #7: Use of the Limits Apex Methods to Avoid Hitting Governor Limits Best Practice #8: Appropriately Best Practice #9: Writing Test Methods to Verify Large Datasets Best Practices #10: Avoid Hardcoding IDs Source:
23
Questions / Comments My favorite TV shows:
24
References Salesforce Best Practices: Eclipse Salesforce IDE: Mavensmate: APEX-LANG: SOQL Builder: Spring ‘15 Release Notes:
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.