Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /home1/oijoiv2f/public_html/wp-content/themes/entaro/template-posts/single/inner.php on line 23

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”);

accountList.add(A1);

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.

Next: 

Sumit Datta

Sumit Datta

I am a 5x Certified Salesforce developer with overall 7 years of IT experience and 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, Salesforce Lightning and Salesforce Configuration.

2 Comments

  1. […] Playing with sObject Collections […]

  2. […] Setting methods. Playing with sObject Collections | SalesforceNextGen. Playing with sObject Collections I cannot imagine a single day of apex developer where he/she does […]

Leave a Comment

Your email address will not be published.