About SalesForce
SalesForce is a cloud-based online solution for customer relationship management or CRM. SalesForce provides all of our departments like marketing, sales, commerce, and service with a shared view of our customers with a single integrated CRM platform.
Apex is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on SalesForce servers in conjunction with calls to the API. Using syntax that looks like Java and acts like database stored procedures, Apex enables developers to add business logic to most system events, including button clicks, related record updates, and Visualforce pages. Apex code can be initiated by Web service requests and from triggers on objects. – Salesforce
Apex Collections is a type of variable, it can store multiple numbers of records. The different types of Collections are, List, Set and Map.
A list is like an array, a sequential collection of elements with first index position as zero. List can contain elements of primitive types, sObjects, user-defined objects, Apex objects or even other collections. A list can contain up to four levels of nested collections. List can contain duplicate elements
Reading this article, you can learn and test the Apex List and Methods in SalesForce.
The prerequisites for testing Apex Lists in SalesForce as,
Register a Salesforce Free Trial account using the following link.
Step 1
Log into your SalesForce account and click the Developer Console.

Step 2
Now, we can create a new Apex Class ApexList file named as ApexList.apxc,

After creating Apex class ApexList, add a Listtest method for creating and testing the List and its methods.

The General Syntax for Apex List is,
- List<DataType> variablename = new List<DataType>();
Apex DataType please refer the link.
- List<String> Dept = new List<String>{'CSE','EEE','ECE'};
- system.debug('Default Department List : '+Dept);
Step 3
Next, we can add the Apex List important predefined methods like add(ListElement), add(index, ListElement), addAll(fromList), size(), clear(), get(index), isEmpty(), and clone().
The first method is add(ListElement) – using this method, we can insert an element into the list. The code is,
- Dept.add('MECH');
- Dept.add('IT');
- system.debug('Using add(ListElement) - Department List : '+Dept);
Next Method is, add(index, ListElement) - using this method, we can insert the element at the given index number. The code is,
- Dept.add(0,'Civil');
- Dept.add(5,'Bio Technology');
- system.debug('Using add(index, ListElement)-Department List : '+Dept);
Next Method is, addAll(fromList) - using this method, we can add all the elements in the list specified, and both lists should be of the same type.
- List<String> Depttest=new List<string>();
- Depttest.addAll(Dept);
- system.debug('Using addAll(fromList) - Department List : '+Depttest);
Next Method is,size() - using this method, we can get the total number of elements in the list.
- Integer s=Dept.size();
- system.debug('Using size() - Department Size : '+s);




Join the conversation! Your thoughts help the community grow.