Wednesday , December 13 2017
Home / Apex / Apex Programming for beginners / Playing with sObject Collections

Playing with sObject Collections

Playing with sObject Collections

I cannot imagine a single day of apex developer where he/she does not use sObject collection in the development. They are the core part of Apex coding when it comes to writing efficient code. sObject collections help in minimizing the probability to hit the governor limit.


First, in the collections come List – or list of sObject

A list can be of any data type but in this post, we are only dealing with sObject or custom objects. Lists are used when we want to process data in bulk. As we all the transaction limits of Apex popularly known as Governor Limit.


List can be used with DML Operations, they can store the data returned by a SOQL Query. A list can be created by using the below code :


List<Account> accountList = new List<Account>();

List<abc__c> custObjList__c = new List< abc__c >();


List can also be populated using a SOQL query while declaring it as below:


List<Account> accountList = [select Id, name from account limit 10];


Elements or new record can be added to a created list and once a record is added to the list, it gets indexed.


List<Account> accountList = new List<Account>();

Account A1 = new Account(Name = “Test Account”);


Account A2 = accountList.get(0);


Records can be processed in bulk if we pass the list variable to the DML Operation as below:

Insert accountList;


Sorting the List of sObject

To sort a list of sObject we can make use of a predefined method list.sort.


Default order of sort is as below:-

  1. First in the order of label
  2. Then on name field if applicable
  3. Then standard field in alphabetical order except id and name field
  4. Lastly on custom field in alphabetical order


For instance, assume two records have a similar name and indistinguishable standard fields, and there are two custom fields, FieldA and FieldB, the value of FieldA is utilized first to sort.


For custom sort order kindly have a look at the below link :


Custom Sort Order


Next Comes Sets in the Collection

Sets only allow unique entity in its confinement, elements added in sets do not get indexed. Set have method like contains or contains all. It is used to determine the uniqueness of sObject.

Ex: Set<Account> accountSet = new Set<Account>{a1, a2};


Finally Map of sObject

Map is the collection of key value pair, and data can be retrieved with the help of the key. It can hold sObject in both key as well as value.


Similar to List map can also be populated using SOQL

Ex: Map<ID, Account> mapOfAccount = new Map<ID, Account>([SELECT Id,  fieldA__c FROM Account])


Use case of map can be assumed as the in memory join of tables or relation of two tables.



About Sumit Datta

Sumit Datta

I am a 3x Certified Salesforce developer with overall 4.5 years of IT experience and 2.5 years of Implementation experience in Salesforce. I am here to share my knowledge and help Beginners in Salesforce to understand the concepts of Apex, Visualforce and Salesforce Configuration.

Check Also


Dreamforce Global Gathering 2017, my experience and why one should not miss this event.

Dreamforce Global Gathering 2017, my experience and why one should not miss this event. First …

Salesforce AppBuilder Certification

Salesforce AppBuilder Certification Mock Exam 1

Want to test your preparation for the Salesforce AppBuilder Certification?   Salesforce AppBuilder Certification mock Exam …

Salesforce Platform Developer 1 Certification

Salesforce Platform Developer 1 Certification Mock Exam 2

Want to test your preparation for the Salesforce Platform Developer 1 Certification? Then go through …

Advanced Formula, Salesforce Platform unleashed!!!

Advanced Formulas Introduction to formula fields and Advanced Formulas Formula Field is a powerful capability …


Leave a Reply

Your email address will not be published. Required fields are marked *