List in Salesforce example

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.

Log into your SalesForce account and click the Developer Console.

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,

  1. Listvariablename=newList[];
Apex DataType please refer the link.
The Dept List creation Code is,
  1. ListDept=newList{'CSE','EEE','ECE'};
  2. system.debug['DefaultDepartmentList:'+Dept];

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['Usingadd[ListElement]-DepartmentList:'+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,'BioTechnology'];
  3. system.debug['Usingadd[index,ListElement]-DepartmentList:'+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. ListDepttest=newList[];
  2. Depttest.addAll[Dept];
  3. system.debug['UsingaddAll[fromList]-DepartmentList:'+Depttest];

Next Method is,size[] - using this method, we can get the total number of elements in the list.

  1. Integers=Dept.size[];
  2. system.debug['Usingsize[]-DepartmentSize:'+s];

Next Method is get[index] - using this method, it returns the element which is at given index.

  1. stringstr=Dept.get[3];
  2. system.debug['Usingget[index]-DepartmentIndex3is:'+str];

Next Method is isEmpty[] - using this method, It returns true if the list is empty.

  1. Booleanb=Dept.isEmpty[];
  2. system.debug['UsingisEmpty[]-DepartmentEmpty:'+b];

Next Method is clone[] - using this method , we can create another copy of the list.

  1. ListDepttest2=newList[];
  2. Depttest2=Dept.clone[];
  3. system.debug['Usingclone[]-DepartmentList2:'+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['Usingclear[]-DepartmentList2:'+Depttest2];
Finally, the ApexList.apxc class code is,
  1. publicclassApexList{
  2. publicstaticvoidListtest[]{
  3. ListDept=newList{
  4. 'CSE',
  5. 'EEE',
  6. 'ECE'
  7. };
  8. system.debug['DefaultDepartmentList:'+Dept];
  9. Dept.add['MECH'];
  10. Dept.add['IT'];
  11. system.debug['Usingadd[ListElement]-DepartmentList:'+Dept];
  12. Dept.add[0,'Civil'];
  13. Dept.add[5,'BioTechnology'];
  14. system.debug['Usingadd[index,ListElement]-DepartmentList:'+Dept];
  15. ListDepttest=newList[];
  16. Depttest.addAll[Dept];
  17. system.debug['UsingaddAll[fromList]-DepartmentList:'+Depttest];
  18. Integers=Dept.size[];
  19. system.debug['Usingsize[]-DepartmentSize:'+s];
  20. stringstr=Dept.get[3];
  21. system.debug['Usingget[index]-DepartmentIndex3is:'+str];
  22. Booleanb=Dept.isEmpty[];
  23. system.debug['UsingisEmpty[]-DepartmentEmpty:'+b];
  24. ListDepttest2=newList[];
  25. Depttest2=Dept.clone[];
  26. system.debug['Usingclone[]-DepartmentList2:'+Depttest2];
  27. Depttest2.clear[];
  28. system.debug['Usingclear[]-DepartmentList2:'+Depttest2];
  29. }
  30. }

For Debugging the Apex class ApexList, click the Debug menu and select Open Execute Anonymous Window.

Now, write the Apex code for calling Listtest[]method and enable the Open log option for viewing output and click Execute.

Now, we can verify the output. After clicking "Execute", the log will be open automatically and select the Debug Only option.

Now, you have successfully tested the Apex List and its methods in SalesForce environment.

Video liên quan

Chủ Đề