Today's Offer - Salesforce Certification Training - Enroll at Flat 10% Off.

- Salesforce Blogs -

What is Collection in Salesforce?

What is Collection in Salesforce?

The collection is the type variables which 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 list element based on Index Number. The list can contain Duplicates. A list should be declared with “List” keyword. Todefine 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. Methods in list

There are few methods available in list

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

add(listElement) Example:

Index 0 Index 1 Index 2 Index 3 Index 4
Daniel Philips Josh Michel Samuel  

  From the index number, we can find the Name

Read: What is OWD in Salesforce?

List<String>mylist = new List<String>();
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);
list<string>allName= new list<string>();
allName.addall(mylist);
string name=mylist.get(1);
system.debug(name);.//Philips

What is collection in Salesforce Click on Debug result What is collection in Salesforce addAll(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<String> firstList = new List<String>();
       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<String> secondList = new List<String>();
       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);
       }
    }

}

Debug result What is collection in Salesforce Remove ():  It removes the list element stored at specific index, returning the element that was removed 


List<String> firstList = new List<String>();
       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<String> firstList = new List<String>();
       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 CC What is collection in Salesforce Clear (); This method removes all elements from a list, consequently setting the list size to zero.

Read: VisualForce Component: Type of VisualForce Component in Salesforce

List<String> firstList = new List<String>();
       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 Salesforce Clone ():

This method will make a duplicate copy of list


List<String> firstList = new List<String>();
       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<String> secondList = new List<String>();
         secondList=firstList.clone();
        system.debug(secondList);

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


List<String> firstList = new List<String>();
       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<String> secondList = new List<String>();
         secondList.add('Justin');  
         secondList.add('FRANK');
         secondList.add('ALBERT');
        Boolean result;
       result=secondList.equals(firstList);
       system.debug(result);

What is collection in Salesforce addAll(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.


List<String> listStrings = new List<String> {'A', 'B', 'D', 'C'};
 Set<String> setString = new Set<String> {'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

Read: Salesforce Marketing Cloud: A Powerful Marketing Platform

public class listDemoEx {
  public List<cAccount> accList {get; set;}
  public List<Account> selectedAccounts{get; set;}
  public List<cAccount> getAccounts(){
        if(accList == null){
            accList = new List<cAccount>();
            for(Account acc : [select Id, Name, Phone from Account limit 25]){
                  accList.add(new cAccount(acc));
            }
         }
       return accList;
   }  
  public PageReference processSelected(){    
        selectedAccounts= new List<Account>();
        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;
        }
   }

}

Visualforce Page


<apex:page controller="listDemoEx" sidebar="true" showHeader="true">
<apex:form >
<apex:pageBlock title="ACCOUNT">
<apex:pageBlockButtons >
<apex:commandButton value="Selected Item" action="{!processSelected}" rerender="out"/>
</apex:pageBlockButtons>
<apex:pageBlockSection columns="2">
<apex:pageBlockTable value="{!Accounts}" var="Acc" columnsWidth="150px,150px" align="left">
<apex:column >
<apex:inputCheckbox value="{!Acc.selected}"/>
</apex:column>
<apex:column value="{!Acc.con.Name}" />
<apex:column value="{!Acc.con.Phone}" />
</apex:pageBlockTable>
<apex:pageBlockTable value="{!selectedAccounts}" var="Rec" id="out" align="right" title="Selected Accounts">
<apex:column headerValue="Account Name">
<apex:outputField value="{!Rec.name}"/>
</apex:column>
<apex:column headerValue="Phone">
<apex:outputField value="{!Rec.Phone}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

What is collection in Salesforce


    Janbask Training

    JanBask Training is a leading Global Online Training Provider through Live Sessions. The Live classes provide a blended approach of hands on experience along with theoretical knowledge which is driven by certified professionals.


Trending Courses

AWS

  • AWS & Fundamentals of Linux
  • Amazon Simple Storage Service
  • Elastic Compute Cloud
  • Databases Overview & Amazon Route 53

Upcoming Class

8 days 14 Dec 2019

DevOps

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

Upcoming Class

9 days 15 Dec 2019

Data Science

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

Upcoming Class

9 days 15 Dec 2019

Hadoop

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

Upcoming Class

-0 day 06 Dec 2019

Salesforce

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

Upcoming Class

14 days 20 Dec 2019

Course for testing

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

Upcoming Class

18 days 24 Dec 2019

QA

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

Upcoming Class

3 days 09 Dec 2019

Business Analyst

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

Upcoming Class

-0 day 06 Dec 2019

SQL Server

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

Upcoming Class

3 days 09 Dec 2019

Comments

Search Posts

Reset

Receive Latest Materials and Offers on Salesforce Course

Interviews