This blog is helpful for SFDC developers or admins to understand SalesForce data collection variable - LIST. It explains all basics and mostly used operations on LIST.
Every programming language (like C, C++, python, apex) has
Below is an incorrect way of getting account records. It can throw an error if zero records are returned by the SOQL query and we try to do some operation on 'acc' variable.
Logs Output:
2. list.addAll(another list): add contents of an already existing List to a List.
3. list.add(index, element): add element at a specific index
Log Output:
String cities = 'Mumbai;Pune;Gurgaon;Noida;New York;Toronto';// multi picklist field value or semi comma separated values in a string
List<String> citiesList = cities.split(';');
system.debug( 'size: ' + citiesList.size());// Output - Size: 6
citiesList.sort();
boolean flag = citiesList.contains('Pune');
system.debug( 'flag: ' + flag);//output- true
string x = citiesList.toString();
system.debug('X: ' + x); //output- (Gurgaon, Mumbai, New York, Noida, Pune, Toronto)
For all other methods about LIST, refer apex developer guide - link
Every programming language (like C, C++, python, apex) has
- Primitive data types like String, Integer etc.
- Collection variables like an array.
- List (ordered)
- Set (unique values and un-ordered)
- Map (key-value pair)
LIST:
An ordered collection of elements, which is identified by their index. A list can contain any number of records of primitive, collections, sObjects, user defined and built in Apex type.
Example to get the basic idea about LIST
Requirement 1: Store name of class topper
String topperName = 'Sudhanshu'; // Storing single value
Requirement 2: store names of all class students
List<String> studentsName = new List<String>{'Sudhanshu','Sachin','Gaurav','Mariya','Amit'}; // storing collection of string.
Here in requirement example 2, students' names are saved in the list in an ordered way like the index of 'Sudhanshu' is 0, 'Sachin' is 1, and so on.
Here in requirement example 2, students' names are saved in the list in an ordered way like the index of 'Sudhanshu' is 0, 'Sachin' is 1, and so on.
How to declare a List?
1. List<String> colorsList = new List<String>(); // Creates a new instance of SET which can hold some value.
2. List<String> colors = new List<String>{'Yellow','Red','Green'}; // with default values
3. List<Integer> rollNumber = new List<Integer>{2,5,7}; // with default value - integer
3. List<Integer> rollNumber = new List<Integer>{2,5,7}; // with default value - integer
3. List<Account> accList = new list<Account>(); // list of type account.
4. List<Account> accList = [Select Id, Name, Industry, Type From ACCOUNT where Industry = 'Utilities'];
Below is an incorrect way of getting account records. It can throw an error if zero records are returned by the SOQL query and we try to do some operation on 'acc' variable.
Account acc = [Select id, Name from accountwhere Industry = 'Utilities'];// Incorrect way
Mostly used methods /functions of LIST
1. List Null check
Note: List must be initialized before applying a null check.List<String> colorsList = new List<String>{'Yellow','Red','Green'};
if (colorsList.size() > 0){
// Do something
}
if (!isEmpty()){
// Do something
}
Logs Output:
| List null check output |
2. Looping and sorting values in a List
Use list.sort(); for sorting elements in ascending orderList<String> studentsName = new List<String>{'Sudhanshu', 'Sachin','Gaurav','Mariya','Amit'};Output
for( String sName : studentsName){
system.debug('Student Name: ' + sName);
}
studentsName.sort();// Sorting in ascending order
for(String sName : studentsName){
system.debug('Student Name after sorting: ' + sName);
}
3. Adding an element to a List.
1. list.add(element): adds a single element to a list2. list.addAll(another list): add contents of an already existing List to a List.
3. list.add(index, element): add element at a specific index
List<String> studentsName = new List<String>();
studentsName.add('Sudhanshu');
studentsName.add('Sachin');
List<String> studentslist2 = new List<String>();
studentslist2.add('Gaurav');
studentslist2.add('Mariya');
studentsName.addAll(studentslist2);// Addting existing list
studentsName.add(1,'Amit');// adding element at a specific index
system.debug('size of studentsName list: ' + studentsName.size());
for( String sName : studentsName){
system.debug('Student Name: ' + sName);
}
Log Output:
| List: Add element logs output |
4. Converting a String into a List, check if any element exists or not, Converting a list to string
When we query multi picklist field value from an object it returns a string separated by ';' semi comma. If we need to apply some conditions or sorting or any manipulation then we can directly convert a string to a List.String cities = 'Mumbai;Pune;Gurgaon;Noida;New York;Toronto';// multi picklist field value or semi comma separated values in a string
List<String> citiesList = cities.split(';');
system.debug( 'size: ' + citiesList.size());// Output - Size: 6
citiesList.sort();
boolean flag = citiesList.contains('Pune');
system.debug( 'flag: ' + flag);//output- true
string x = citiesList.toString();
system.debug('X: ' + x); //output- (Gurgaon, Mumbai, New York, Noida, Pune, Toronto)
Contains(): is used to check if any element is present in List or not
ToString(): is used to convert List values directly into a ',' comma-separated string.
5. Clear all values in List
Clear: removes all the values from a List.
Example: citiesList.clear();
6. Referring the first element of List returned by SOQL query
List<Account> accList = [Select Id, Name, Industry, Type From ACCOUNT where Industry = 'Utilities' Limit 1];
String accName = accList[0].Name; // Getting field value
7. Get Index of an element and Get element value by Index
List<String> myStrings = new List<String>{'a', 'b', 'a'};
String myVal = myStrings.get(0);
System.debug('myVal :' + myVal); // output - a
Integer indexValue = myStrings.indexOf('a');
System.debug('indexValue of a: ' + indexValue);// output - 0
For all other methods about LIST, refer apex developer guide - link
Keep Learning... Keep Growing !!! - Sudhanshu Gupta
No comments:
Post a Comment