SlideShare a Scribd company logo
Apex Best Practices
Josep Vall-llovera - CTO at Clapps
19/06/2015
@valnavjo Valnavjo in/jvn84 Valnavjo
Introduction
2
#1 Naming conventions
3
1. SF doesn’t provide packages to organize classes.
2. Use naming conventions (English).
#2 Singleton pattern
4
1. Same instance per execution request.
2. Useful to avoid repeating unnecessary queries.
#3 Util classes
5
1. Always use Singleton Pattern + Utils class.
2. For example, Record Types.
3. Do not query the same RT more than once.
6
#4 Trigger Template
1. Do not have more than one Trigger for the same
SObject.
2. Salesforce doesn’t ensure the order of execution.
3. Avoid chaos.
7
#5 Bulkify
1. All logic methods must be prepared to deal with
collections.
#6 Group collections
8
1. Use Maps & Lists to group collections when bulkifying
code.
#7 Interfaces
9
1. Use interfaces when they are necessary.
2. Focus on productivity.
#8 Unit Tests
10
1. Unit Tests must be UNIT.
2. Avoid @seeAllData = true.
a. For Pricebooks, use Test.getStandardPricebookId().
3. Use public class with @IsTest for data initialization.
4. Just three things: prepare data, do test and assertions.
#9 Use System.Limits
1. Avoid governor limits.
2. For example, when performing a search with SOSL.
11
#10 Keep things simple
12
Logic
TriggerHandler
Utils
UI
API Helper
Trigger
Controller
Service
Unit
Tests
Useful links
1. Apex Code Best Practices
2. Context Variable
3. Triggers
4. My Trigger Template
5. Use of the Limits Apex Methods
13
Code resources
https://github.com/Valnavjo/sdgbarcelona.git
14
Thanks!
Q&A
15

More Related Content

Similar to June SDG - Apex Best Practices

Perfomatix - iOS swift coding standards
Perfomatix - iOS swift coding standardsPerfomatix - iOS swift coding standards
Perfomatix - iOS swift coding standards
Perfomatix Solutions
 
Unit Testing Best Practices
Unit Testing Best PracticesUnit Testing Best Practices
Unit Testing Best Practices
Tomaš Maconko
 
Testing 2: Advanced Test Management
Testing 2: Advanced Test Management Testing 2: Advanced Test Management
Testing 2: Advanced Test Management
Inflectra
 
Hadoop testing workshop - july 2013
Hadoop testing workshop - july 2013Hadoop testing workshop - july 2013
Hadoop testing workshop - july 2013
Ophir Cohen
 
Code smells quality of code
Code smells   quality of codeCode smells   quality of code
Code smells quality of code
Hasan Al Mamun
 
35 Excel Tips That Could Save You from Working All Night
35 Excel Tips That Could Save You from Working All Night35 Excel Tips That Could Save You from Working All Night
35 Excel Tips That Could Save You from Working All Night
Power System Operation
 
35 MS Office Excel Tips
35 MS Office Excel Tips35 MS Office Excel Tips
35 MS Office Excel Tips
Power System Operation
 
Delphi - Howto Threads
Delphi - Howto ThreadsDelphi - Howto Threads
Delphi - Howto Threads
Niall Munro
 
The Ring programming language version 1.8 book - Part 81 of 202
The Ring programming language version 1.8 book - Part 81 of 202The Ring programming language version 1.8 book - Part 81 of 202
The Ring programming language version 1.8 book - Part 81 of 202
Mahmoud Samir Fayed
 
PuppetConf 2016: Turning Pain Into Gain: A Unit Testing Story – Nadeem Ahmad ...
PuppetConf 2016: Turning Pain Into Gain: A Unit Testing Story – Nadeem Ahmad ...PuppetConf 2016: Turning Pain Into Gain: A Unit Testing Story – Nadeem Ahmad ...
PuppetConf 2016: Turning Pain Into Gain: A Unit Testing Story – Nadeem Ahmad ...
Puppet
 
Assignment 8 using arrays/tutorialoutlet
Assignment 8 using arrays/tutorialoutletAssignment 8 using arrays/tutorialoutlet
Assignment 8 using arrays/tutorialoutlet
Repton
 
Learn Terraform - Variables
Learn Terraform - VariablesLearn Terraform - Variables
Learn Terraform - Variables
StephaneBoghossian1
 
ATXPUG Meetup 11/11/14 - Managing complexity in Puppet Code
ATXPUG Meetup 11/11/14 - Managing complexity in Puppet CodeATXPUG Meetup 11/11/14 - Managing complexity in Puppet Code
ATXPUG Meetup 11/11/14 - Managing complexity in Puppet Code
Byron Miller
 
Ot performance webinar
Ot performance webinarOt performance webinar
Ot performance webinar
Suite Solutions
 
Nodejs & Typescript
Nodejs & TypescriptNodejs & Typescript
Nodejs & Typescript
Knoldus Inc.
 
Test driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practicesTest driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practices
Narendra Pathai
 
WIT Ajmer Event - Apex code best practices
WIT Ajmer Event - Apex code best practicesWIT Ajmer Event - Apex code best practices
WIT Ajmer Event - Apex code best practices
Ranu Bari
 
Easy to use correctly, hard to use incorrectly
Easy to use correctly, hard to use incorrectlyEasy to use correctly, hard to use incorrectly
Easy to use correctly, hard to use incorrectly
Christophe Addinquy
 
Master class in modern Java
Master class in modern JavaMaster class in modern Java
Master class in modern Java
Miro Cupak
 
To Loop or Not to Loop: Overcoming Roadblocks with FME
To Loop or Not to Loop: Overcoming Roadblocks with FMETo Loop or Not to Loop: Overcoming Roadblocks with FME
To Loop or Not to Loop: Overcoming Roadblocks with FME
Safe Software
 

Similar to June SDG - Apex Best Practices (20)

Perfomatix - iOS swift coding standards
Perfomatix - iOS swift coding standardsPerfomatix - iOS swift coding standards
Perfomatix - iOS swift coding standards
 
Unit Testing Best Practices
Unit Testing Best PracticesUnit Testing Best Practices
Unit Testing Best Practices
 
Testing 2: Advanced Test Management
Testing 2: Advanced Test Management Testing 2: Advanced Test Management
Testing 2: Advanced Test Management
 
Hadoop testing workshop - july 2013
Hadoop testing workshop - july 2013Hadoop testing workshop - july 2013
Hadoop testing workshop - july 2013
 
Code smells quality of code
Code smells   quality of codeCode smells   quality of code
Code smells quality of code
 
35 Excel Tips That Could Save You from Working All Night
35 Excel Tips That Could Save You from Working All Night35 Excel Tips That Could Save You from Working All Night
35 Excel Tips That Could Save You from Working All Night
 
35 MS Office Excel Tips
35 MS Office Excel Tips35 MS Office Excel Tips
35 MS Office Excel Tips
 
Delphi - Howto Threads
Delphi - Howto ThreadsDelphi - Howto Threads
Delphi - Howto Threads
 
The Ring programming language version 1.8 book - Part 81 of 202
The Ring programming language version 1.8 book - Part 81 of 202The Ring programming language version 1.8 book - Part 81 of 202
The Ring programming language version 1.8 book - Part 81 of 202
 
PuppetConf 2016: Turning Pain Into Gain: A Unit Testing Story – Nadeem Ahmad ...
PuppetConf 2016: Turning Pain Into Gain: A Unit Testing Story – Nadeem Ahmad ...PuppetConf 2016: Turning Pain Into Gain: A Unit Testing Story – Nadeem Ahmad ...
PuppetConf 2016: Turning Pain Into Gain: A Unit Testing Story – Nadeem Ahmad ...
 
Assignment 8 using arrays/tutorialoutlet
Assignment 8 using arrays/tutorialoutletAssignment 8 using arrays/tutorialoutlet
Assignment 8 using arrays/tutorialoutlet
 
Learn Terraform - Variables
Learn Terraform - VariablesLearn Terraform - Variables
Learn Terraform - Variables
 
ATXPUG Meetup 11/11/14 - Managing complexity in Puppet Code
ATXPUG Meetup 11/11/14 - Managing complexity in Puppet CodeATXPUG Meetup 11/11/14 - Managing complexity in Puppet Code
ATXPUG Meetup 11/11/14 - Managing complexity in Puppet Code
 
Ot performance webinar
Ot performance webinarOt performance webinar
Ot performance webinar
 
Nodejs & Typescript
Nodejs & TypescriptNodejs & Typescript
Nodejs & Typescript
 
Test driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practicesTest driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practices
 
WIT Ajmer Event - Apex code best practices
WIT Ajmer Event - Apex code best practicesWIT Ajmer Event - Apex code best practices
WIT Ajmer Event - Apex code best practices
 
Easy to use correctly, hard to use incorrectly
Easy to use correctly, hard to use incorrectlyEasy to use correctly, hard to use incorrectly
Easy to use correctly, hard to use incorrectly
 
Master class in modern Java
Master class in modern JavaMaster class in modern Java
Master class in modern Java
 
To Loop or Not to Loop: Overcoming Roadblocks with FME
To Loop or Not to Loop: Overcoming Roadblocks with FMETo Loop or Not to Loop: Overcoming Roadblocks with FME
To Loop or Not to Loop: Overcoming Roadblocks with FME
 

Recently uploaded

Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
VALiNTRY360
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
Peter Muessig
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
Bert Jan Schrijver
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
Alina Yurenko
 
Top 9 Trends in Cybersecurity for 2024.pptx
Top 9 Trends in Cybersecurity for 2024.pptxTop 9 Trends in Cybersecurity for 2024.pptx
Top 9 Trends in Cybersecurity for 2024.pptx
devvsandy
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
Remote DBA Services
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
mz5nrf0n
 
SQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure MalaysiaSQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure Malaysia
GohKiangHock
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
ICS
 
Malibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed RoundMalibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed Round
sjcobrien
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
Yara Milbes
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Julian Hyde
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
Marcin Chrost
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 

Recently uploaded (20)

Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdfTop Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
Top Benefits of Using Salesforce Healthcare CRM for Patient Management.pdf
 
UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
 
Top 9 Trends in Cybersecurity for 2024.pptx
Top 9 Trends in Cybersecurity for 2024.pptxTop 9 Trends in Cybersecurity for 2024.pptx
Top 9 Trends in Cybersecurity for 2024.pptx
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
 
SQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure MalaysiaSQL Accounting Software Brochure Malaysia
SQL Accounting Software Brochure Malaysia
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
Webinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for EmbeddedWebinar On-Demand: Using Flutter for Embedded
Webinar On-Demand: Using Flutter for Embedded
 
Malibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed RoundMalibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed Round
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 

June SDG - Apex Best Practices