SlideShare a Scribd company logo
Apex Collection Design Pattern
Sathishkumar Periyasamy
Twitter : @ppksathish1
Facebook : https://www.facebook.com/periyasamy.sathish
LinkedIn : www.linkedin.com/in/sathishkumar-periyasamy-00039117
Agenda
• What is Collections?
• Type of collections
• Explain List, Set, Map
• Collection Patterns
• Summary
What is collection?
• Collection is nothing but an array(call it other computer language).
• Collection is an object that can hold reference of other object or
sObject.
• Strongly typed as object or sObject.
• There is no limitation to store the value on the collection but think
about heap size issue.
Type of Collection
• There are 3 type of collection :
• List
• Set
• Map
List
• List is basic type of array(other computer language).
• List can hold primitive, object, sObject
• Stored in ordered received data.
• sObject List can be sort by Name and Standard Fields.
• DML operation allowed sObject List.
• List allow duplicate entries.
• Can be used used in SOQL query in the filter condition and store query result.
• 2 way you can declare list in apex code.
• List<String> lstStrting = new List<Strting>();
• String[] aryString;
• 2 way you can access list in apex code.
• lstString.get(i);
• aryString[i];
List Example
List of sObject:
List<Account> lstAccount = new List<Account>();
lstAccount = [Select Id, Name from Account limit 10];
List<Contact> lstContact = new List<Contact>();
lstContact = [Select Id, AccountId from Contact where AccountID IN:lstAccount ];
Another Way (Using single query to get both account and contact):
List<Account> lstAccount = new List<Account>();
lstAccount = [Select Id, Name, (Select Id, AccountId from Contacts) from Account
limit 10];
List Example
Primitive List:
List<String> lstString = new List<String>();
List<account> lstAccount = [Select Id, Name from Account limit 10];
for(Account iterator : lstAccount) lstString .add(iterator.Name);
Object List (Use wrapper class on the List):
List< wrapper> lstWrapper = new List< wrapper>();
List<account> lstAccount = [Select Id, Name from Account limit 10];
for(Account iterator : lstAccount) {
lstWrapper.add(new wrapper(iterator.Name, iterator.Id));
}
Wrapper Class:
Public class wrapper {
String strName;
Id accountID;
public wrapper (String strAccName, Id accountID){
this.strName = strAccName;
this. accountID = accountID
}
}
Set
• Set is basically a hash table.
• Set item should be unique.
• Data not stored in order receiving.
• Set can hold primitive, object, sObject - Better to stay with primitive
type.
• Can be used used in SOQL query in the filter condition.
• Set can be pass to the list constructor.
Set Example
Set of sObject:
Set<Account> setAccount = new set<Account>();
Account objAccount = new Account(Name = ‘Salesforce’);
setAccount.add(objAccount);
objAccount.Name = ‘Force.com’;
setAccount.add(objAccount);
Result : Set size will be “2” because Set is unique.
Note : you should be very careful while using sObject in the Set.
Set Example
Set of primitive data type:
Set<Id> setAccount = new set<Id>();
List<Account> lstAccount = [Selecte Id from Account];
for(Account iterator : lstAccount) {
setAccount.add(iterator.Id);
}
Set<String> setAccountFormatching = new set<String>();
List<Account> lstAccount = [Selecte Id, Name, Email from Account];
for(Account iterator : lstAccount) {
setAccount.add(iterator.Name + ’’ + iterator.Email);
}
Note : you can concatenate many fields into single string. This can be used for some kind
of matching logic.
Map
• Map is also hash table with Key and Value pair.
• Key is Unique.
• Value is associated values to that Key. This can hold – List, Set, Map,
Primitive data type, object and sObject.
• You can’t use map in the DML/SOQL Query directory
• Use Map.Values() method in DML – Value should be sObject.
• Use Map.keySet() method in SOQL query.
Map Example
Map of sObject:
Map<Id, Account> mapAccount = new Map<Id, Account>([Select Id, Name from
Account where….......]);
System.debug(‘Only Id : ’+ mapAccount.keySet()); -- use this in soql query.
System.debug(‘Only List of Account : ’+ mapAccount.values()); -- use this in DML.
Nested Map:
Map<Id, Map<Id, Map<String, …>>> mapAccount = new Map<Id, Map<Id,
Map<String, …>>>();
Note : Nested Map used for most complex business logic.
Collection Patterns
• When you want to pull contact my account Id. Want to store contact for particular account Id.
Pattern 1:
List<Contact> lstContact = [Select Id, accountid from contact where accountid IN:setAccountId];
Map<Id, List<Contact>> mapContactByAccount = new Map<Id, List<Contact>> ();
for(Contact iterator : lstContact) {
if(!mapContactByAccount.contacsKey(iterator.AccountID)) {
mapContactByAccount.put(iterator.AccountID, new List<Contact>());
}
mapContactByAccount.get(iterator.AccountID).add(iterator);
}
Pattern 2:
List<Account> lstAccount = [Select Id, (Select Id, Name from Contacts) from account where ID IN:setAccountId];
Map<Id, List<Contact>> mapContactByAccount = new Map<Id, List<Contact>> ();
for(Account iterator : lstAccount) {
iteratorChild.put(iterator.Id, iterator.Contacts);
}
Note : Pattern 1 will take more script line execution but Pattern 2 will take less script line execution.
Summary
• Talked about type of collection.
https://developer.salesforce.com/docs/atlas.en-
us.apexcode.meta/apexcode/langCon_apex_collections.htm
• Collection patters
- Go through Dan Appleman online video -
https://app.pluralsight.com/library/courses/apex-fundamentals/table-of-
contents
Thank You
Ad

More Related Content

What's hot (20)

SOQL & SOSL for Admins
SOQL & SOSL for AdminsSOQL & SOSL for Admins
SOQL & SOSL for Admins
Obidjon Komiljonov
 
Getting started with Salesforce security
Getting started with Salesforce securityGetting started with Salesforce security
Getting started with Salesforce security
Salesforce Admins
 
Introduction to Apex Triggers
Introduction to Apex TriggersIntroduction to Apex Triggers
Introduction to Apex Triggers
Salesforce Developers
 
Salesforce Streaming event - PushTopic and Generic Events
Salesforce Streaming event - PushTopic and Generic EventsSalesforce Streaming event - PushTopic and Generic Events
Salesforce Streaming event - PushTopic and Generic Events
Dhanik Sahni
 
Learn to Leverage the Power of SOQL
Learn to Leverage the Power of SOQLLearn to Leverage the Power of SOQL
Learn to Leverage the Power of SOQL
Salesforce Admins
 
Exploring the Salesforce REST API
Exploring the Salesforce REST APIExploring the Salesforce REST API
Exploring the Salesforce REST API
Salesforce Developers
 
Sharing and setting in salesforce
Sharing and setting in salesforceSharing and setting in salesforce
Sharing and setting in salesforce
Vishesh Singhal
 
OAuth with Salesforce - Demystified
OAuth with Salesforce - DemystifiedOAuth with Salesforce - Demystified
OAuth with Salesforce - Demystified
Calvin Noronha
 
MySQL: Indexing for Better Performance
MySQL: Indexing for Better PerformanceMySQL: Indexing for Better Performance
MySQL: Indexing for Better Performance
jkeriaki
 
Salesforce administrator training presentation slides
Salesforce administrator training presentation slides Salesforce administrator training presentation slides
Salesforce administrator training presentation slides
Salesforce Associates
 
Dynamic input tables lwc vs aura vs. visualforce
Dynamic input tables  lwc vs aura vs. visualforceDynamic input tables  lwc vs aura vs. visualforce
Dynamic input tables lwc vs aura vs. visualforce
Mike Tetlow
 
Web api
Web apiWeb api
Web api
Sudhakar Sharma
 
Web Components and Security
Web Components and SecurityWeb Components and Security
Web Components and Security
Tyler Peterson
 
Lecture 4 sql {basics keys and constraints}
Lecture 4 sql {basics  keys and constraints}Lecture 4 sql {basics  keys and constraints}
Lecture 4 sql {basics keys and constraints}
Shubham Shukla
 
Introduction to Apex for Developers
Introduction to Apex for DevelopersIntroduction to Apex for Developers
Introduction to Apex for Developers
Salesforce Developers
 
Sql Basics And Advanced
Sql Basics And AdvancedSql Basics And Advanced
Sql Basics And Advanced
rainynovember12
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
Stewart Rogers
 
Sharing and security in Salesforce
Sharing and security in SalesforceSharing and security in Salesforce
Sharing and security in Salesforce
Saurabh Kulkarni
 
Salesforce Security Model (Dmitry Goshko, Igor Haritonovich)
Salesforce Security Model (Dmitry Goshko, Igor Haritonovich)Salesforce Security Model (Dmitry Goshko, Igor Haritonovich)
Salesforce Security Model (Dmitry Goshko, Igor Haritonovich)
Yury Bondarau
 
Integrating with salesforce
Integrating with salesforceIntegrating with salesforce
Integrating with salesforce
Mark Adcock
 
Getting started with Salesforce security
Getting started with Salesforce securityGetting started with Salesforce security
Getting started with Salesforce security
Salesforce Admins
 
Salesforce Streaming event - PushTopic and Generic Events
Salesforce Streaming event - PushTopic and Generic EventsSalesforce Streaming event - PushTopic and Generic Events
Salesforce Streaming event - PushTopic and Generic Events
Dhanik Sahni
 
Learn to Leverage the Power of SOQL
Learn to Leverage the Power of SOQLLearn to Leverage the Power of SOQL
Learn to Leverage the Power of SOQL
Salesforce Admins
 
Sharing and setting in salesforce
Sharing and setting in salesforceSharing and setting in salesforce
Sharing and setting in salesforce
Vishesh Singhal
 
OAuth with Salesforce - Demystified
OAuth with Salesforce - DemystifiedOAuth with Salesforce - Demystified
OAuth with Salesforce - Demystified
Calvin Noronha
 
MySQL: Indexing for Better Performance
MySQL: Indexing for Better PerformanceMySQL: Indexing for Better Performance
MySQL: Indexing for Better Performance
jkeriaki
 
Salesforce administrator training presentation slides
Salesforce administrator training presentation slides Salesforce administrator training presentation slides
Salesforce administrator training presentation slides
Salesforce Associates
 
Dynamic input tables lwc vs aura vs. visualforce
Dynamic input tables  lwc vs aura vs. visualforceDynamic input tables  lwc vs aura vs. visualforce
Dynamic input tables lwc vs aura vs. visualforce
Mike Tetlow
 
Web Components and Security
Web Components and SecurityWeb Components and Security
Web Components and Security
Tyler Peterson
 
Lecture 4 sql {basics keys and constraints}
Lecture 4 sql {basics  keys and constraints}Lecture 4 sql {basics  keys and constraints}
Lecture 4 sql {basics keys and constraints}
Shubham Shukla
 
Sharing and security in Salesforce
Sharing and security in SalesforceSharing and security in Salesforce
Sharing and security in Salesforce
Saurabh Kulkarni
 
Salesforce Security Model (Dmitry Goshko, Igor Haritonovich)
Salesforce Security Model (Dmitry Goshko, Igor Haritonovich)Salesforce Security Model (Dmitry Goshko, Igor Haritonovich)
Salesforce Security Model (Dmitry Goshko, Igor Haritonovich)
Yury Bondarau
 
Integrating with salesforce
Integrating with salesforceIntegrating with salesforce
Integrating with salesforce
Mark Adcock
 

Viewers also liked (20)

Apex Design Patterns
Apex Design PatternsApex Design Patterns
Apex Design Patterns
Salesforce Developers
 
Salesforce Coding techniques that keep your admins happy (DF13)
Salesforce Coding techniques that keep your admins happy (DF13)Salesforce Coding techniques that keep your admins happy (DF13)
Salesforce Coding techniques that keep your admins happy (DF13)
Roy Gilad
 
Batch Apex in Salesforce
Batch Apex in SalesforceBatch Apex in Salesforce
Batch Apex in Salesforce
David Helgerson
 
Building strong foundations apex enterprise patterns
Building strong foundations apex enterprise patternsBuilding strong foundations apex enterprise patterns
Building strong foundations apex enterprise patterns
andyinthecloud
 
Dependency Injection with Apex
Dependency Injection with ApexDependency Injection with Apex
Dependency Injection with Apex
Salesforce Developers
 
Development lifecycle guide (part 1)
Development lifecycle guide (part 1)Development lifecycle guide (part 1)
Development lifecycle guide (part 1)
Abdelhakim Mouttaqui ☁
 
Apex Enterprise Patterns: Building Strong Foundations
Apex Enterprise Patterns: Building Strong FoundationsApex Enterprise Patterns: Building Strong Foundations
Apex Enterprise Patterns: Building Strong Foundations
Salesforce Developers
 
Governor limits
Governor limitsGovernor limits
Governor limits
Shivanath Devinarayanan
 
From Sandbox To Production: An Introduction to Salesforce Release Management
From Sandbox To Production: An Introduction to Salesforce Release ManagementFrom Sandbox To Production: An Introduction to Salesforce Release Management
From Sandbox To Production: An Introduction to Salesforce Release Management
Salesforce Developers
 
Secure Development on the Salesforce Platform - Part 3
Secure Development on the Salesforce Platform - Part 3Secure Development on the Salesforce Platform - Part 3
Secure Development on the Salesforce Platform - Part 3
Mark Adcock
 
Advanced Apex Development - Asynchronous Processes
Advanced Apex Development - Asynchronous ProcessesAdvanced Apex Development - Asynchronous Processes
Advanced Apex Development - Asynchronous Processes
Salesforce Developers
 
Salesforce Release Management - Best Practices and Tools for Deployment
Salesforce Release Management - Best Practices and Tools for DeploymentSalesforce Release Management - Best Practices and Tools for Deployment
Salesforce Release Management - Best Practices and Tools for Deployment
Salesforce Developers
 
Secure Development on the Salesforce Platform - Part I
Secure Development on the Salesforce Platform - Part ISecure Development on the Salesforce Platform - Part I
Secure Development on the Salesforce Platform - Part I
Salesforce Developers
 
The Ideal Salesforce Development Lifecycle
The Ideal Salesforce Development LifecycleThe Ideal Salesforce Development Lifecycle
The Ideal Salesforce Development Lifecycle
Joshua Hoskins
 
Salesforce Data Structures
Salesforce Data StructuresSalesforce Data Structures
Salesforce Data Structures
Idealist Consulting
 
Salesforce.com process map (from lead to opportunity)
Salesforce.com process map (from lead to opportunity)Salesforce.com process map (from lead to opportunity)
Salesforce.com process map (from lead to opportunity)
Roger Borges Grilo
 
Secure Development on the Salesforce Platform - Part 2
Secure Development on the Salesforce Platform - Part 2Secure Development on the Salesforce Platform - Part 2
Secure Development on the Salesforce Platform - Part 2
Salesforce Developers
 
Enterprise Architecture Salesforce
Enterprise Architecture SalesforceEnterprise Architecture Salesforce
Enterprise Architecture Salesforce
Peter Doolan
 
Introduction to Campaigns in Salesforce - Create, Manage, Launch, and Measure
Introduction to Campaigns in Salesforce - Create, Manage, Launch, and MeasureIntroduction to Campaigns in Salesforce - Create, Manage, Launch, and Measure
Introduction to Campaigns in Salesforce - Create, Manage, Launch, and Measure
Shell Black
 
Data model in salesforce
Data model in salesforceData model in salesforce
Data model in salesforce
Chamil Madusanka
 
Salesforce Coding techniques that keep your admins happy (DF13)
Salesforce Coding techniques that keep your admins happy (DF13)Salesforce Coding techniques that keep your admins happy (DF13)
Salesforce Coding techniques that keep your admins happy (DF13)
Roy Gilad
 
Batch Apex in Salesforce
Batch Apex in SalesforceBatch Apex in Salesforce
Batch Apex in Salesforce
David Helgerson
 
Building strong foundations apex enterprise patterns
Building strong foundations apex enterprise patternsBuilding strong foundations apex enterprise patterns
Building strong foundations apex enterprise patterns
andyinthecloud
 
Apex Enterprise Patterns: Building Strong Foundations
Apex Enterprise Patterns: Building Strong FoundationsApex Enterprise Patterns: Building Strong Foundations
Apex Enterprise Patterns: Building Strong Foundations
Salesforce Developers
 
From Sandbox To Production: An Introduction to Salesforce Release Management
From Sandbox To Production: An Introduction to Salesforce Release ManagementFrom Sandbox To Production: An Introduction to Salesforce Release Management
From Sandbox To Production: An Introduction to Salesforce Release Management
Salesforce Developers
 
Secure Development on the Salesforce Platform - Part 3
Secure Development on the Salesforce Platform - Part 3Secure Development on the Salesforce Platform - Part 3
Secure Development on the Salesforce Platform - Part 3
Mark Adcock
 
Advanced Apex Development - Asynchronous Processes
Advanced Apex Development - Asynchronous ProcessesAdvanced Apex Development - Asynchronous Processes
Advanced Apex Development - Asynchronous Processes
Salesforce Developers
 
Salesforce Release Management - Best Practices and Tools for Deployment
Salesforce Release Management - Best Practices and Tools for DeploymentSalesforce Release Management - Best Practices and Tools for Deployment
Salesforce Release Management - Best Practices and Tools for Deployment
Salesforce Developers
 
Secure Development on the Salesforce Platform - Part I
Secure Development on the Salesforce Platform - Part ISecure Development on the Salesforce Platform - Part I
Secure Development on the Salesforce Platform - Part I
Salesforce Developers
 
The Ideal Salesforce Development Lifecycle
The Ideal Salesforce Development LifecycleThe Ideal Salesforce Development Lifecycle
The Ideal Salesforce Development Lifecycle
Joshua Hoskins
 
Salesforce.com process map (from lead to opportunity)
Salesforce.com process map (from lead to opportunity)Salesforce.com process map (from lead to opportunity)
Salesforce.com process map (from lead to opportunity)
Roger Borges Grilo
 
Secure Development on the Salesforce Platform - Part 2
Secure Development on the Salesforce Platform - Part 2Secure Development on the Salesforce Platform - Part 2
Secure Development on the Salesforce Platform - Part 2
Salesforce Developers
 
Enterprise Architecture Salesforce
Enterprise Architecture SalesforceEnterprise Architecture Salesforce
Enterprise Architecture Salesforce
Peter Doolan
 
Introduction to Campaigns in Salesforce - Create, Manage, Launch, and Measure
Introduction to Campaigns in Salesforce - Create, Manage, Launch, and MeasureIntroduction to Campaigns in Salesforce - Create, Manage, Launch, and Measure
Introduction to Campaigns in Salesforce - Create, Manage, Launch, and Measure
Shell Black
 
Ad

Similar to Apex collection patterns (20)

standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++
•sreejith •sree
 
lect 2-DS ALGO(online).pdf
lect 2-DS  ALGO(online).pdflect 2-DS  ALGO(online).pdf
lect 2-DS ALGO(online).pdf
MuhammadUmerIhtisham
 
JavaScript.pptx
JavaScript.pptxJavaScript.pptx
JavaScript.pptx
pramod599939
 
11. Programming(BS-phy6)-Lecture11+12 .pdf
11. Programming(BS-phy6)-Lecture11+12 .pdf11. Programming(BS-phy6)-Lecture11+12 .pdf
11. Programming(BS-phy6)-Lecture11+12 .pdf
UmarIslam14
 
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptxLecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
yhrcxd8wpm
 
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptxLecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
yhrcxd8wpm
 
data structures queue stack insert and delete time complexity
data structures queue stack insert and delete time complexitydata structures queue stack insert and delete time complexity
data structures queue stack insert and delete time complexity
libannpost
 
11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptx
ssuser8e50d8
 
8.DATA STRUCTURES UNIT 1 AND 2 CS3301PPT.pptx
8.DATA STRUCTURES UNIT 1 AND 2 CS3301PPT.pptx8.DATA STRUCTURES UNIT 1 AND 2 CS3301PPT.pptx
8.DATA STRUCTURES UNIT 1 AND 2 CS3301PPT.pptx
venigkrish89
 
Abstract Algebra and Category Theory
Abstract Algebra and Category Theory Abstract Algebra and Category Theory
Abstract Algebra and Category Theory
Naveenkumar Muguda
 
Unit 3_Numpy_Vsp.pptx
Unit 3_Numpy_Vsp.pptxUnit 3_Numpy_Vsp.pptx
Unit 3_Numpy_Vsp.pptx
prakashvs7
 
collections
 collections collections
collections
Yaswanth Babu Gummadivelli
 
Groovy
GroovyGroovy
Groovy
NexThoughts Technologies
 
Introduction to R _IMPORTANT FOR DATA ANALYTICS
Introduction to R _IMPORTANT FOR DATA ANALYTICSIntroduction to R _IMPORTANT FOR DATA ANALYTICS
Introduction to R _IMPORTANT FOR DATA ANALYTICS
HaritikaChhatwal1
 
Collections
CollectionsCollections
Collections
Manav Prasad
 
14_linked list_updated-updated-updated(1).pdf
14_linked list_updated-updated-updated(1).pdf14_linked list_updated-updated-updated(1).pdf
14_linked list_updated-updated-updated(1).pdf
dipanshutiwari1155
 
Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...
nsitlokeshjain
 
8 python data structure-1
8 python data structure-18 python data structure-1
8 python data structure-1
Prof. Dr. K. Adisesha
 
12888239 (2).ppt
12888239 (2).ppt12888239 (2).ppt
12888239 (2).ppt
SrinivasanCSE
 
Collections in Java
Collections in JavaCollections in Java
Collections in Java
Khasim Cise
 
standard template library(STL) in C++
standard template library(STL) in C++standard template library(STL) in C++
standard template library(STL) in C++
•sreejith •sree
 
11. Programming(BS-phy6)-Lecture11+12 .pdf
11. Programming(BS-phy6)-Lecture11+12 .pdf11. Programming(BS-phy6)-Lecture11+12 .pdf
11. Programming(BS-phy6)-Lecture11+12 .pdf
UmarIslam14
 
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptxLecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
yhrcxd8wpm
 
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptxLecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
Lecture 1 Abstract Data Types of Complexity Analysis of Big Oh Notation.pptx
yhrcxd8wpm
 
data structures queue stack insert and delete time complexity
data structures queue stack insert and delete time complexitydata structures queue stack insert and delete time complexity
data structures queue stack insert and delete time complexity
libannpost
 
11 Introduction to lists.pptx
11 Introduction to lists.pptx11 Introduction to lists.pptx
11 Introduction to lists.pptx
ssuser8e50d8
 
8.DATA STRUCTURES UNIT 1 AND 2 CS3301PPT.pptx
8.DATA STRUCTURES UNIT 1 AND 2 CS3301PPT.pptx8.DATA STRUCTURES UNIT 1 AND 2 CS3301PPT.pptx
8.DATA STRUCTURES UNIT 1 AND 2 CS3301PPT.pptx
venigkrish89
 
Abstract Algebra and Category Theory
Abstract Algebra and Category Theory Abstract Algebra and Category Theory
Abstract Algebra and Category Theory
Naveenkumar Muguda
 
Unit 3_Numpy_Vsp.pptx
Unit 3_Numpy_Vsp.pptxUnit 3_Numpy_Vsp.pptx
Unit 3_Numpy_Vsp.pptx
prakashvs7
 
Introduction to R _IMPORTANT FOR DATA ANALYTICS
Introduction to R _IMPORTANT FOR DATA ANALYTICSIntroduction to R _IMPORTANT FOR DATA ANALYTICS
Introduction to R _IMPORTANT FOR DATA ANALYTICS
HaritikaChhatwal1
 
14_linked list_updated-updated-updated(1).pdf
14_linked list_updated-updated-updated(1).pdf14_linked list_updated-updated-updated(1).pdf
14_linked list_updated-updated-updated(1).pdf
dipanshutiwari1155
 
Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...
nsitlokeshjain
 
Collections in Java
Collections in JavaCollections in Java
Collections in Java
Khasim Cise
 
Ad

Recently uploaded (20)

DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptxDevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
DevOpsDays Atlanta 2025 - Building 10x Development Organizations.pptx
Justin Reock
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Electronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploitElectronic_Mail_Attacks-1-35.pdf by xploit
Electronic_Mail_Attacks-1-35.pdf by xploit
niftliyevhuseyn
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Generative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in BusinessGenerative Artificial Intelligence (GenAI) in Business
Generative Artificial Intelligence (GenAI) in Business
Dr. Tathagat Varma
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded DevelopersLinux Support for SMARC: How Toradex Empowers Embedded Developers
Linux Support for SMARC: How Toradex Empowers Embedded Developers
Toradex
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
Transcript: #StandardsGoals for 2025: Standards & certification roundup - Tec...
BookNet Canada
 

Apex collection patterns

  • 1. Apex Collection Design Pattern Sathishkumar Periyasamy Twitter : @ppksathish1 Facebook : https://www.facebook.com/periyasamy.sathish LinkedIn : www.linkedin.com/in/sathishkumar-periyasamy-00039117
  • 2. Agenda • What is Collections? • Type of collections • Explain List, Set, Map • Collection Patterns • Summary
  • 3. What is collection? • Collection is nothing but an array(call it other computer language). • Collection is an object that can hold reference of other object or sObject. • Strongly typed as object or sObject. • There is no limitation to store the value on the collection but think about heap size issue.
  • 4. Type of Collection • There are 3 type of collection : • List • Set • Map
  • 5. List • List is basic type of array(other computer language). • List can hold primitive, object, sObject • Stored in ordered received data. • sObject List can be sort by Name and Standard Fields. • DML operation allowed sObject List. • List allow duplicate entries. • Can be used used in SOQL query in the filter condition and store query result. • 2 way you can declare list in apex code. • List<String> lstStrting = new List<Strting>(); • String[] aryString; • 2 way you can access list in apex code. • lstString.get(i); • aryString[i];
  • 6. List Example List of sObject: List<Account> lstAccount = new List<Account>(); lstAccount = [Select Id, Name from Account limit 10]; List<Contact> lstContact = new List<Contact>(); lstContact = [Select Id, AccountId from Contact where AccountID IN:lstAccount ]; Another Way (Using single query to get both account and contact): List<Account> lstAccount = new List<Account>(); lstAccount = [Select Id, Name, (Select Id, AccountId from Contacts) from Account limit 10];
  • 7. List Example Primitive List: List<String> lstString = new List<String>(); List<account> lstAccount = [Select Id, Name from Account limit 10]; for(Account iterator : lstAccount) lstString .add(iterator.Name); Object List (Use wrapper class on the List): List< wrapper> lstWrapper = new List< wrapper>(); List<account> lstAccount = [Select Id, Name from Account limit 10]; for(Account iterator : lstAccount) { lstWrapper.add(new wrapper(iterator.Name, iterator.Id)); } Wrapper Class: Public class wrapper { String strName; Id accountID; public wrapper (String strAccName, Id accountID){ this.strName = strAccName; this. accountID = accountID } }
  • 8. Set • Set is basically a hash table. • Set item should be unique. • Data not stored in order receiving. • Set can hold primitive, object, sObject - Better to stay with primitive type. • Can be used used in SOQL query in the filter condition. • Set can be pass to the list constructor.
  • 9. Set Example Set of sObject: Set<Account> setAccount = new set<Account>(); Account objAccount = new Account(Name = ‘Salesforce’); setAccount.add(objAccount); objAccount.Name = ‘Force.com’; setAccount.add(objAccount); Result : Set size will be “2” because Set is unique. Note : you should be very careful while using sObject in the Set.
  • 10. Set Example Set of primitive data type: Set<Id> setAccount = new set<Id>(); List<Account> lstAccount = [Selecte Id from Account]; for(Account iterator : lstAccount) { setAccount.add(iterator.Id); } Set<String> setAccountFormatching = new set<String>(); List<Account> lstAccount = [Selecte Id, Name, Email from Account]; for(Account iterator : lstAccount) { setAccount.add(iterator.Name + ’’ + iterator.Email); } Note : you can concatenate many fields into single string. This can be used for some kind of matching logic.
  • 11. Map • Map is also hash table with Key and Value pair. • Key is Unique. • Value is associated values to that Key. This can hold – List, Set, Map, Primitive data type, object and sObject. • You can’t use map in the DML/SOQL Query directory • Use Map.Values() method in DML – Value should be sObject. • Use Map.keySet() method in SOQL query.
  • 12. Map Example Map of sObject: Map<Id, Account> mapAccount = new Map<Id, Account>([Select Id, Name from Account where….......]); System.debug(‘Only Id : ’+ mapAccount.keySet()); -- use this in soql query. System.debug(‘Only List of Account : ’+ mapAccount.values()); -- use this in DML. Nested Map: Map<Id, Map<Id, Map<String, …>>> mapAccount = new Map<Id, Map<Id, Map<String, …>>>(); Note : Nested Map used for most complex business logic.
  • 13. Collection Patterns • When you want to pull contact my account Id. Want to store contact for particular account Id. Pattern 1: List<Contact> lstContact = [Select Id, accountid from contact where accountid IN:setAccountId]; Map<Id, List<Contact>> mapContactByAccount = new Map<Id, List<Contact>> (); for(Contact iterator : lstContact) { if(!mapContactByAccount.contacsKey(iterator.AccountID)) { mapContactByAccount.put(iterator.AccountID, new List<Contact>()); } mapContactByAccount.get(iterator.AccountID).add(iterator); } Pattern 2: List<Account> lstAccount = [Select Id, (Select Id, Name from Contacts) from account where ID IN:setAccountId]; Map<Id, List<Contact>> mapContactByAccount = new Map<Id, List<Contact>> (); for(Account iterator : lstAccount) { iteratorChild.put(iterator.Id, iterator.Contacts); } Note : Pattern 1 will take more script line execution but Pattern 2 will take less script line execution.
  • 14. Summary • Talked about type of collection. https://developer.salesforce.com/docs/atlas.en- us.apexcode.meta/apexcode/langCon_apex_collections.htm • Collection patters - Go through Dan Appleman online video - https://app.pluralsight.com/library/courses/apex-fundamentals/table-of- contents