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

Apex collection patterns

  • 1.
    Apex Collection DesignPattern Sathishkumar Periyasamy Twitter : @ppksathish1 Facebook : https://www.facebook.com/periyasamy.sathish LinkedIn : www.linkedin.com/in/sathishkumar-periyasamy-00039117
  • 2.
    Agenda • What isCollections? • 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 isbasic 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 ofsObject: 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 isbasically 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 ofsObject: 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 ofprimitive 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 isalso 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 ofsObject: 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 • Whenyou 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 abouttype 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
  • 15.