List Collection:

List is an Ordered collection, Which preserves the insertion order. i.e. The elements will be available in the same order, in which they were inserted. List Collection allows to store both Homogeneous and Heterogeneous Elements. List Collection supports Dynamic Memory Allocation.

List Collection allows us to store the elements of Type “Primitive Type, SObject Type, Collection Type, Apex Type and User Defined Type”. Each Element in the List Collection can be identified with the help of an Index Position, which starts from Zero.

Note: List Collection Allows us to store Duplicate Elements also.

Syntax: List<DataType> <objectName> = new List<DataType>();


               List<Integer> customerCodes = new List<Integer>();

               List<string> countryNames = new List<String>();

               List<ID> recordIds = new List<ID>();

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

               List<Case> lstCases = new List<Case>();

               List<Customer__C> lstCustomers = new List<Customer__C>();

               List<Policy__C> lstPolicies = new List<Policy__C>();

               List<SObject> lstRecords = new List<SObject>();

               List<Object> lstElements = new List<Object>();

Note:     We can create the Nested List Collections upto 5 Levels.    

List<List<List<List<List<String>>>>>lstElements=new                                                        LList<List<List<List<List<String>>>>>();


   1. Add(<ElementName>):

     Syntax:   <objectName>.Add(<ElementName>);

  2. Add(<Integer IndexPosition>, <ElementName>)              

  3. AddAll(<CollectionName>):

  Ex: string[] customerDetails = new string[]

{‘Rajesh’,’Sampath’,’Kiran’,”Suresh’}; customerNames.AddAll(customerDetails);             

  4. Get(<IndexPosition>):

Ex: system.debug(‘Element exist at the position 4 is..: ‘+ customerNames.Get(4));

5. Size():

    Ex: customerNames.Size();  O/p: 8

system.debug(‘Collection size is….: ‘+ customerNames.size());                                     

6. Boolean Contains(<ElementName>):

  Ex: TRUE –> Element Found.

      FALSE –> Element Not Found in the collection.


                                             system.debug(‘Element Found.’);


                                             system.debug(‘Element Not Found.’);

7. Remove(<IndexPosition>):

    Ex:  customerNames.Remove(3);

        |Ramesh |  TCS | Kiran |  Rajesh | Sampath | Kiran | Suresh |

0 1 2 3 4 5 6

8. Clear():

Ex: customerNames.Clear();

9. Boolean IsEmpty():

            TRUE –> When the Collection is Empty.

               FALSE –> Collection is Not Empty.



         system.debug(‘Collection is Empty.’);


         system.debug(‘Collection Contains Elements.’);

10. IndexOf(<ElementName>):


            customerNames.IndexOf(‘Reddy Labs’); –> -1

         customerNames.IndexOf(‘Kiran’); –> 5

          if(customerNames.Contains(‘Reddy Labs’))


              system.debug(‘Index Position is…: ‘+ customerNames.IndexOf(‘Reddy Labs’));




              system.debug(‘Element Not Found. Invalid Element Name.’);


   11. Clone():

     Ex:   List<String> backupCopy = customerNames.Clone();

   12. Sort():

     Ex: customerNames.Sort();

    13. Set(<IndexPosition>,<NewElementName>):

       Ex: customerNames.Set(2, ‘Rajesh Tripathi’);

Bulkification / Bulkify Process:

Upon performing the DML operations through programming, salesforce has given the Governor Limit, that we can use max. of 150 DML Statements in a single Transaction.

When the user tries to utilize more than 150 DML statements inside a Transaction, salesforce will raise an exception “System.Limit Exception. Too Many DML Statements 151”. And the Whole transaction will Rolled Back.

To avoid these issues, we have to use “Bulkification / Bulkify Process”.

Step 1: Create a List Collection, to Hold the Records.

  Ex: List<Account> lstAccounts = new List<Account>();

Step 2: Create the Account Records, and Add the Records to the Collection by using FOR Loop.


        for(integer counter = 1; counter <= 10; counter++)


           Account acc = new Account();

         acc.Name = ‘Sample Test Account’;

           acc.Rating = ‘Hot’;

           acc.AnnualRevenue = 1200000;

           acc.Industry = ‘Finance’;

           acc.Active__C = ‘Yes’;

// Add the Record to the Collection.



Step 3: Once the records has been added to the collection, then perform the DML operations on the Collection.


     if(! lstAccounts.isEmpty())


      insert lstAccounts;


Best Practices:

1. Avoid the Usage of DML Statements inside the FOR Loop.

2. Upon performing the DML operations, always use the “Bulkification Process”.

Governor Limits:

1. We can use max. of 150 DML Statements(INSERT, UPDATE, DELETE, UNDELETE, UPSERT, MERGE) within a Transaction.

2. By using each DML Statement, we can process max. of 10,000 records at a time.