Sunday , February 25 2018
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 5 years of IT experience and 3 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, Salesforce Lightning and Salesforce Configuration.

Check Also

Salesforce Lightning Interview Questions

Top 50 Salesforce Lightning Interview Questions 2018

Salesforce Lightning Interview Questions Below is the comprehensive list of questions asked in Salesforce Lightning …

Trailhead Leaderboard (Unofficial)

Trailhead Leaderboard(Unofficial)

Trailhead Leaderboard (Unofficial) Hello everyone, Today I am presenting to you Salesforce Trailhead LeaderBoard(Unofficial), a …

Salesforce Trailhead Widget for your Website

Salesforce Trailhead widget for your Website Hi Friends Recently, I have been working on a small …

Salesforce Lightning tutorials

Salesforce Lightning Tutorial: Lightning Component basics

Salesforce Lightning Tutorial: Lightning Component basics Chapter 1: Prerequisite for Lightning Component Development My Domain …


Leave a Reply

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