International Womens Day : Flat 30% off on live classes + 2 free self-paced courses - SCHEDULE CALL

- Salesforce Blogs -

What is the Collection in Salesforce?

Introduction

The collection is the type of variables that can store multiple numbers of records. It can increase and decrease dynamically.

There are 3 types of collection

  1. List Collection
  2. Set Collection
  3. Map Collection

List Collection

It creates a new instance of the List class. A list can hold elements of any data type. A list is an interface. A list is an ordered collection of typed primitives, sObjects, user-defined objects, Apex objects or collections that are distinguished by their indices. A list is an ordered collection so use list when you want to identify the list element based on Index Number. The list can contain Duplicates. A list should be declared with “List” keyword. To define a list use list keyword by primitive data, sObject within <> characters. The index position of the first element in a list always starts with [0].it can grow dynamically at the run. It allows duplicate and null values.

Learn Salesforce in the Easiest Way

  • Learn from the videos
  • Learn anytime anywhere
  • Pocket-friendly mode of learning
  • Complimentary eBook available
Buy Self-learning at Discounted Price

Methods in list

There are few methods available in the list

  • add(list Element)
  • add(index, list Element)
  • addAll(from List)
  • addAll(from Set)
  • clear()
  • clone()
  • Remove()
  • Size()
  • equals(list2)

add(listElement) Example:

Read: A Basic Overview for Salesforce Security
Index 0 Index 1 Index 2 Index 3 Index 4
Daniel Philips Josh Michel Samuel  

  From the index number, we can find the Name


Listmylist = new List();
string n1= ‘Daniel’;
string n2= ‘Philips’;
string n3= ‘Josh’;
string n4=’ Michel’;
string n5=’ Samuel’;
mylist.add(n1);
mylist.add(n2);
mylist.add(n3);
mylist.add(n4);
mylist.add(n5);
listallName= new list();
allName.addall(mylist);
string name=mylist.get(1);
system.debug(name);.//Philips

What is collection in SalesforceClick on Debug resultWhat is collection in SalesforceaddAll(from List): Addall of the elements in the specified list to the list that calls the method. Both lists must be of the same type.


public class ExampleOfList {
      public static void addAlltest() {
       List firstList = new List();
       firstList.add('Justin');
       firstList.add('FRANK');
       firstList.add('LOUIS');
       for(string name1:firstList){
      System.debug('First List Details is-->'+ ''+name1);
   }
     system.debug('First List size is-->'+ firstList.size());
       List secondList = new List();
       secondList.add('ALBERT');
       secondList.add('JACKSON');
       secondList.add('FREDDIE');
       secondList.addAll(firstList);
          system.debug('Both list names are '+' '+secondList+''+secondList.size());
          for(String name : secondList){
           System.debug('Second List Details is-->'+ ''+name);
       }
    }

}

Salesforce Training For Administrators & Developers

  • No cost for a Demo Class
  • Industry Expert as your Trainer
  • Available as per your schedule
  • Customer Support Available
demo class

Debug resultWhat is collection in SalesforceRemove ():  It removes the list element stored at specific index, returning the element that was removed 

Read: Salesforce Administrator | Salesforce Admin Skills, Importance & How to Become a Salesforce Admin

List firstList = new List();
       firstList.add('FREDDIE');
       firstList.add('JACKSON');
       firstList.add('ALBERT');
       firstList.remove(2); //remove 2nd position value.
       for(String name : firstList){
           System.debug(name);
       }
    }
Debug result //ALBERT removed from list

salesforce quiz

Sort();

This method s sorts list in ascending order.


List firstList = new List();
       firstList.add('Justin');
       firstList.add('FRANK');
       firstList.add('LOUIS');
       firstList.add('ALBERT');
       firstList.add('JACKSON');
       firstList.add('FREDDIE');
       for(string name1:firstList){
      System.debug('First List Details is-->'+ ''+name1);
   }
         firstList.sort();
          system.debug('Names are in alphabate from '+' '+firstList);

Debug result CCWhat is collection in SalesforceClear (); This method removes all elements from a list, consequently setting the list size to zero.


List firstList = new List();
       firstList.add('Justin');
       firstList.add('FRANK');
       firstList.add('LOUIS');
       firstList.add('ALBERT');
       firstList.add('JACKSON');
       firstList.add('FREDDIE');
       for(string name1:firstList){
           System.debug('First List Details is-->'+ ''+name1);
   }
         firstList.clear();
          system.debug('list size is '+' '+firstList);

What is collection in SalesforceClone ():

This method will make a duplicate copy of list


List firstList = new List();
       firstList.add('Justin');
       firstList.add('FRANK');
       firstList.add('LOUIS');
       firstList.add('ALBERT');
       firstList.add('JACKSON');
       firstList.add('FREDDIE');
       for(string name1:firstList){
           System.debug('First List Details is-->'+ ''+name1);
   }
      List secondList = new List();
         secondList=firstList.clone();
        system.debug(secondList);

What is collection in SalesforceEquals(List); Compares the list with the specific list and returns true if both are equals, otherwise, returns false.


List firstList = new List();
       firstList.add('Justin');
       firstList.add('FRANK');
       firstList.add('LOUIS');
       firstList.add('ALBERT');
       firstList.add('JACKSON');
       firstList.add('FREDDIE');
       for(string name1:firstList){
           System.debug('First List Details is-->'+ ''+name1);
   }
      List secondList = new List();
         secondList.add('Justin');  
         secondList.add('FRANK');
         secondList.add('ALBERT');
        Boolean result;
       result=secondList.equals(firstList);
       system.debug(result);

What is collection in SalesforceaddAll(Set) : Add all of the elements in specified set to the list that calls the method. The set and the list must be of the same type.

Read: Interview With Eric Dreshfield

List listStrings = new List {'A', 'B', 'D', 'C'};
 Set setString = new Set {'F', 'V', 'Z'};
 listStrings.addAll(setString);
system.debug(listStrings);
Debug output is |DEBUG| (A, B, D, C, F, V, Z)

Example How to display selected record in another section using list Apex class


public class listDemoEx {
  public List accList {get; set;}
  public List selectedAccounts{get; set;}
  public List getAccounts(){
        if(accList == null){
            accList = new List();
            for(Account acc : [select Id, Name, Phone from Account limit 25]){
                  accList.add(new cAccount(acc));
            }
         }
       return accList;
   }  
  public PageReference processSelected(){    
        selectedAccounts= new List();
        for(cAccount cCon : getAccounts()) {
            if(cCon.selected == true){
                selectedAccounts.add(cCon.con);
            }
        }           
        return null;
   }

  public class cAccount {
        public Account con {get; set;}
        public Boolean selected {get; set;}
        public cAccount(Account c) {
            con = c;
            selected = false;
        }
   }

}

What is collection in Salesforce

Summary

Apex is an intelligent programming platform for Salesforce. You can have a real fun with it by utilizing Collections. Share your creativity with us using the comment section below. Happy coding!


 user

JanBask Training

A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.


Comments

Trending Courses

Cyber Security icon

Cyber Security

  • Introduction to cybersecurity
  • Cryptography and Secure Communication 
  • Cloud Computing Architectural Framework
  • Security Architectures and Models
Cyber Security icon

Upcoming Class

1 day 16 Jun 2025

QA icon

QA

  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing
QA icon

Upcoming Class

-1 day 14 Jun 2025

Salesforce icon

Salesforce

  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL
Salesforce icon

Upcoming Class

6 days 21 Jun 2025

Business Analyst icon

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum
Business Analyst icon

Upcoming Class

6 days 21 Jun 2025

MS SQL Server icon

MS SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design
MS SQL Server icon

Upcoming Class

6 days 21 Jun 2025

Data Science icon

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning
Data Science icon

Upcoming Class

5 days 20 Jun 2025

DevOps icon

DevOps

  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing
DevOps icon

Upcoming Class

5 days 20 Jun 2025

Hadoop icon

Hadoop

  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation
Hadoop icon

Upcoming Class

-1 day 14 Jun 2025

Python icon

Python

  • Features of Python
  • Python Editors and IDEs
  • Data types and Variables
  • Python File Operation
Python icon

Upcoming Class

13 days 28 Jun 2025

Artificial Intelligence icon

Artificial Intelligence

  • Components of AI
  • Categories of Machine Learning
  • Recurrent Neural Networks
  • Recurrent Neural Networks
Artificial Intelligence icon

Upcoming Class

6 days 21 Jun 2025

Machine Learning icon

Machine Learning

  • Introduction to Machine Learning & Python
  • Machine Learning: Supervised Learning
  • Machine Learning: Unsupervised Learning
Machine Learning icon

Upcoming Class

40 days 25 Jul 2025

 Tableau icon

Tableau

  • Introduction to Tableau Desktop
  • Data Transformation Methods
  • Configuring tableau server
  • Integration with R & Hadoop
 Tableau icon

Upcoming Class

-1 day 14 Jun 2025

Interviews