Apex List In SalesForce

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.
 
Apex List In SalesForce
 
Step 2
 
Now, we can create a new Apex Class ApexList file named as ApexList.apxc,
 
Apex List In SalesForce 
Apex List In SalesForce
 
After creating Apex class ApexList, add a Listtest method for creating and testing the List and its methods.
 
Apex List In SalesForce
 
The General Syntax for Apex List is,
  1. List<DataType> variablename = new List<DataType>();  
Apex DataType please refer the link.
 
The Dept List creation Code is,
  1. List<String> Dept = new List<String>{'CSE','EEE','ECE'};  
  2. 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,
  1. Dept.add('MECH');  
  2. Dept.add('IT');  
  3. 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,
  1. Dept.add(0,'Civil');  
  2. Dept.add(5,'Bio Technology');  
  3. 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.
  1. List<String> Depttest=new List<string>();  
  2. Depttest.addAll(Dept);  
  3. 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.
  1. Integer s=Dept.size();  
  2. system.debug('Using size() - Department Size : '+s);  
Next Method is get(index) - using this method, it returns the element which is at given index.
  1. string str=Dept.get(3);  
  2. system.debug('Using get(index) - Department Index 3 is: '+str);  
Next Method is isEmpty() - using this method, It returns true if the list is empty.
  1. Boolean b=Dept.isEmpty();  
  2. system.debug('Using isEmpty() - Department Empty : '+b);  
Next Method is clone() - using this method , we can create another copy of the list.
  1. List<String> Depttest2=new List<string>();  
  2. Depttest2=Dept.clone();  
  3. system.debug('Using clone() - Department List 2 : '+Depttest2);  
Last Method is clear() – This will remove all the elements from the list. Hence the length of that list will be zero.
  1. Depttest2.clear();  
  2. system.debug('Using clear() - Department List 2 : '+Depttest2);  
Finally, the ApexList.apxc class code is,
  1. public class ApexList {  
  2.     public static void Listtest() {  
  3.         List < String > Dept = new List < String > {  
  4.             'CSE',  
  5.             'EEE',  
  6.             'ECE'  
  7.         };  
  8.         system.debug('Default Department List : ' + Dept);  
  9.         //add(ListElement)  
  10.         Dept.add('MECH');  
  11.         Dept.add('IT');  
  12.         system.debug('Using add(ListElement) - Department List : ' + Dept);  
  13.         //add(index, ListElement)  
  14.         Dept.add(0, 'Civil');  
  15.         Dept.add(5, 'Bio Technology');  
  16.         system.debug('Using add(index, ListElement)-Department List : ' + Dept);  
  17.         //addAll(fromList)  
  18.         List < String > Depttest = new List < string > ();  
  19.         Depttest.addAll(Dept);  
  20.         system.debug('Using addAll(fromList) - Department List : ' + Depttest);  
  21.         //size()  
  22.         Integer s = Dept.size();  
  23.         system.debug('Using size() - Department Size : ' + s);  
  24.         //get(index)  
  25.         string str = Dept.get(3);  
  26.         system.debug('Using get(index) - Department Index 3 is: ' + str);  
  27.         //isEmpty()  
  28.         Boolean b = Dept.isEmpty();  
  29.         system.debug('Using isEmpty() - Department Empty : ' + b);  
  30.         //clone()  
  31.         List < String > Depttest2 = new List < string > ();  
  32.         Depttest2 = Dept.clone();  
  33.         system.debug('Using clone() - Department List 2 : ' + Depttest2);  
  34.         //Clear()  
  35.         Depttest2.clear();  
  36.         system.debug('Using clear() - Department List 2 : ' + Depttest2);  
  37.     }  
  38. }  
Step 4
 
For Debugging the Apex class ApexList, click the Debug menu and select Open Execute Anonymous Window.
 
Apex List In SalesForce
 
Now, write the Apex code for calling Listtest()method and enable the Open log option for viewing output and click Execute.
 
Apex List In SalesForce
  1. ApexList.Listtest();  
Step 4
 
Now, we can verify the output. After clicking "Execute", the log will be open automatically and select the Debug Only option.
 
Apex List In SalesForce
 
Summary
 
Now, you have successfully tested the Apex List and its methods in SalesForce environment.


Similar Articles