List Methods in
Salesforce
(Explanation with perfect
examples)
Jai Yadav
What is list in Salesforce?
In Salesforce, specifically in Apex (the programming
language used in Salesforce), a list is a type of
collection that holds an ordered set of elements.
Each element can be accessed by its position or
index in the list, and the order of elements is
preserved as they are added. This allows for
efficient organization and retrieval of data based on
their sequence.
Syntax :-
Different List Methods :-
1. add(listElement)
In Salesforce Apex, the add method is used to insert an
element into a List at the end. When you call add on a list,
it appends the specified item to the end of the existing
list, increasing its size by one.
Output :-
2. add(index, listElement)
In Salesforce Apex, the add(index, listElement) method is
used to insert an element into a List at a specific
position. Unlike the add() method, which appends an
element to the end of the list, add(index, listElement)
allows you to specify exactly where the new element
should go.
Output :-
3. addAll(fromList / fromSet)
Adds all of the elements in the specified list/set to the
list that calls the method. Both lists must be of the same
type.
Output :-
4. clear()
In Salesforce Apex, the clear() method is used to delete
all items from a List, effectively resetting it to an empty
state.
Output :-
5. clone()
In Salesforce Apex, the clone() method can be used to
create a copy of a List. This method creates a new List
with the same elements as the original but as a
separate object, allowing you to modify the new list
without affecting the original one.
Output :-
6. equals(list2)
In Salesforce Apex, the equals(list2) method is used to
compare two lists to check if they are identical. This
method returns true if both lists have the same
elements in the same order, and false otherwise.
Output :-
7. get(index)
In Apex, the get(index) method is used to retrieve an
element from a List at a specific position.
Output :-
8. isEmpty()
In Apex, the isEmpty() method is used to check whether
the list is empty or not. Returns true if list is empty else
retuns false.
Output :-
9. remove(index)
Removes list element at a specified index.
Output :-
10. size()
Returns the number of elements in a list.
Output :-
11. sort()
Sorts the items in a list.
Output :-
12. set(index, listElement)
In Apex, the set(index, listElement) method is used to
update or replace an element in a List at a specified
position. This method allows you to modify the value of
an existing element in the list without changing the size
of the list.
Output :-
I'd love to hear your thoughts on
the Salesforce content we
share! Please leave a review in
the comments and let us know
how we can continue to provide
valuable insights for you.
If you still have any confusion or want to learn
more about Salesforce Admin or Dev, you can
check out my topmate account. Just click the link in
the caption and book a session on topmate.com
now at very affordable prices.
LIKE
COMMENT ON
SHARE
IT
Jai Yadav