How to use list methods in salesforce

List is a collection of elements, and they are ordered according to their insertion; elements can be of any data-type. List allows duplicate values and are referred by their indices. Output of every SOQL(Salesforce Object Query Language) query is a list.


Creating a list: List<DataType> variablename = new List<DataType>();

We can assign the values statically at the time of creating the list,

  List<String> name = new List<String>{'John','Kelly','Mike'};

  Hence values are stored as,

      index pos 0 = John
      index pos 1 = Kelly
      index pos 2 = Mike

In another way, we can assign values dynamically when we are creating a list,

    List<String> name = new List<String>();

Hence the values and index positions are stored as mentioned above.

List has many predefined methods in which we will look into some of them.

add(ListElement): It inserts an element into the list.     

      List<String> name = new List<String>();
add(index, ListElement); It inserts the element at the given index number.

        List<String> name = new List<String>();

Positions will be like, 
         index pos 0=Mike
         index pos 1=Paul
         index pos 2=John
         index pos 3=Kelly

addAll(fromList): It is used to add all the elements in the list specified, and both lists should be of same type. It is represented as,

        Public void addAll(List fromList)

addAll(fromSet): It is used to add all the elements in the set when this method is called, and both set and list elements should be of same type. Represented as,

        Public void addAll(Set fromSet)

size(): It is used to get the total number of elements in the list. Represented as,

        Public integer size()

clear(): It will remove all the elements from the list. Hence the length of that list will be zero.

        Public void clear()

get(index): It returns the element which is at given index.

        Public object get(Integer index)

To reference an element in the list, we can also use the element index position in square brackets such as,

       List<data type> name = new dataype[index position]

isEmpty(): It returns true if the list is empty.

        Public Boolean isEmpty()

clone(): It creates another copy of the list.

        Public List<object> clone()

If the list is of sObject records, the duplicate list can only be a copy of the list. Only the duplicate will have references to each object, but the sObject records themselves will not be duplicated. To duplicate these sObject records, we have to use deepClone method.  

Note: There is no limit for a list to hold any number of records. But there is a limit for heap size, which is 6 Mb for synchronous and 12 Mb for asynchronous.

Above-mentioned methods are some of them and there are other methods.

    Posted On
  • 01 February 2018
  • By
  • Micropyramid

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
How to convert a web page into PDF format using renderAs attribute

Companies frequently need web page data to be converted into pdf. So, they can have an easy look into the progress. For this in salesforce ...

Continue Reading...
How to run a SOQL query in Salesforce

Salesforce Object Query Language(SOQL) is used to fetch the data from Sobject in salesforce organization. As we know SQL is used by .Net developers to ...

Continue Reading...
How to use Data loader in Salesforce

In any business importing and exporting of data is very important. Only to some extent, we can enter the data manually in the fields of ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties