Thursday, 23 April 2020

SalesForce data collection Part 1 - LIST

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

  1. Primitive data types like String, Integer etc.
  2. Collection variables like an array.
Apex (Salesforce programming language) has the following data collection (that can store multiple numbers of records) variables:

  1. List (ordered)
  2. Set (unique values and un-ordered)
  3. 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. 


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<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
}
List null check
List null check
















Logs Output:
List null check output













2. Looping and sorting values in a List

Use list.sort(); for sorting elements in ascending order
List<String> studentsName = new List<String>{'Sudhanshu', 'Sachin','Gaurav','Mariya','Amit'};
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);
}
Output














3. Adding an element to a List.

1. list.add(element): adds a single element to a list
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 
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

LWC Component Bundle | Build your first component | Core Concepts

In my previous blog , I explained all the pre-requisite steps to set up the LWC development environment on your system. Now I'll expl...