apex collection patterns

15
Apex Collection Design Pattern Sathishkumar Periyasamy Twitter : @ppksathish1 Facebook : https://www.facebook.com/periyasamy.sathish LinkedIn : www.linkedin.com/in/sathishkumar- periyasamy-00039117

Upload: sathishkumar-periyasamy

Post on 11-Apr-2017

196 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Apex collection patterns

Apex Collection Design Pattern

Sathishkumar PeriyasamyTwitter : @ppksathish1Facebook : https://www.facebook.com/periyasamy.sathishLinkedIn : www.linkedin.com/in/sathishkumar-periyasamy-00039117

Page 2: Apex collection patterns

Agenda• What is Collections?• Type of collections• Explain List, Set, Map• Collection Patterns• Summary

Page 3: Apex collection patterns

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.

Page 4: Apex collection patterns

Type of Collection• There are 3 type of collection : • List• Set • Map

Page 5: Apex collection patterns

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];

Page 6: Apex collection patterns

List ExampleList 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];

Page 7: Apex collection patterns

List ExamplePrimitive 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

}}

Page 8: Apex collection patterns

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.

Page 9: Apex collection patterns

Set ExampleSet 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.

Page 10: Apex collection patterns

Set ExampleSet 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.

Page 11: Apex collection patterns

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.

Page 12: Apex collection patterns

Map ExampleMap 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.

Page 13: Apex collection patterns

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.

Page 15: Apex collection patterns

Thank You