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

- Salesforce Blogs -

How to Set Methods in Salesforce Apex?

What is set? A set is an unordered collection of elements that contain any type of data type like primitive types, collections, sObjects, user-defined types and built-in Apex types. Set do not allow duplicate .Insertion order is not preserved in the set. It also grows dynamically in run time like list. You cannot perform DML with Set. To declare a set, use the Set keyword followed by the primitive data type name within <> characters. For example


Set<string>mystrings;
Set<account>Acc = new set<account> ();
Set<CustomObject__c>cust= new set<CustomObject __c>();

Set is collection type which contains multiple numbers of unordered unique records. A Set cannot have duplicate records Like Lists. salesforce quiz Set Methods

  • add(setElement)
  • addAll(fromList)
  • addAll(fromSet)
  • clear()
  • clone()
  • contains(setElement)
  • containsAll(listToCompare)
  • containsAll(setToCompare)
  • retainAll(listOfElementsToRetain)

Ø removeAll(setOfElementsToRemove)

  • isempty():

add(setElement): Adds an element to the set if it is not already present.

Read: How To Get A Job In Salesforce Without Experience?

set<string> names= new set<string>();
names.add('Jack');
names.add('Steve');
names.add('jorge');
names.add('Jack');
system.debug(names);
system.debug(names.size());//3

Here we have added ‘jack’ two times but set will not allow duplicate value, so debug output is Jack, Steve and Jorge. addAll(fromset) This method willaddallall value of the set to the other set.


Set<String> state = new Set<String>{'Alaska', 'Arizona ','California','Arkansas' };
set<String> s2 = new set<String>(state);
system.debug(s2);

addAll(fromlist): Adds all of the elements in the specified list to the set if they are not already present.


list<String>mynamelist = new list<String>{'Alaska', 'Arizona ','California','Arkansas' };
set<String> name = new set<String>();
name.add('Virginia');
name.addAll(mynamelist);
system.debug(name);// {Alaska, Arizona , Arkansas, California, Virginia}

Contains (Element); Returns true if the set contains the specified element.

Read: What is Salesforce Architecture? Tutorial Guide for Beginners

Set<String>myString = new Set<String>{'ABC', 'XYZ'};
Boolean result =myString.contains('ABC');
Boolean result1 = myString.contains('AXZ');
System.debug(result);//True
System.debug(result1)//False

containsAll(setToCompare): Returns true if the set contains all of the elements in the specified set.


Set<String>myString = new Set<String>{'a', 'b'};
Set<String>sString = new Set<String>{'c'};
Set<String>rString = new Set<String>{'a', 'b', 'c'};

Boolean result1, result2;
result1 = myString.addAll(sString);
System.debug(result1);//True
result2 = myString.containsAll(rString);
System.debug(result2);// True

retainAll(listOfElementsToRetain): This will keep  only the value that areexisting in list or set..


list<string>Names= new list<string>{'Dell','IBM','Apple'};
set<string>Orglist= new set<string>();
Orglist.add('Dell');
Orglist.add('Sony');
Orglist.add('Acer');
Orglist.add('HP');
boolean result;
result=Orglist.retainall(names);
system.debug(result);//True

removeAll(setOfElementsToRemove)

This method will removes the elements in the specified list from the set if they are present.

Read: What Are The Validation Rules? How To Create Salesforce Validation Rules?

set<integer>values= new set<integer>{10,20,30,40};
set<integer>ages= new set<integer>{10,20,30};
values.removeAll(ages);
system.debug(values);
isempty():

This method return true if the set has no element
set<integer>values= new set<integer>();
set<integer>values1= new set<integer>{10,20};
boolean flag=values.isempty();
boolean flag1=values1.isempty();
system.debug(flag);//True
system.debug(flag1);//false
How to use set in example

Apex class
public class picklistNewExample {
public set<string>name         {set;get;}
public string selected          {set;get;}
public list<account>acclist     {set;get;}
public list<selectOption>option {set;get;}

publicpicklistNewExample(){
option= new list<selectoption>();
selectOption op = new selectOption('none','-None-');
option.add(op);
name= new set<string>();
list<account>ac=[select industry,name,type,phone from account where industry != null];
for(account a:ac){
name.add(a.industry);  

     }
for(string b:name){
SelectOption op1 = new selectOption(b,b);
option.add(op1);
     }

    }
public void DisplayRecord(){
acclist = new list<account>();
acclist = [select name,industry,phone,type from account where industry=:selected];
    }
}

<apex:page controller="picklistNewExample" id="page" >
<apex:sectionHeader title="Account Record" description="PickList" help="help" />
<apex:form id="fm">
<apex:pageblock title="Choose PickList" tabStyle="Lead" id="pb" >
<apex:pageblocksection title="Select One" id="pbs">
<apex:selectList size="1" value="{!selected}">
<apex:selectOptions value="{!option}"/>
<apex:actionSupport event="onchange" action="{!displayRecord}"/>
</apex:selectList>
</apex:pageblocksection>
<apex:pageBlockSection title="Selected Records" rendered="{! !IsNull(acclist)}">
<apex:pageBlockTable value="{!acclist}" var="a"  >
<apex:column value="{!a.name}" />
<apex:column value="{!a.industry}" />
<apex:column value="{!a.type}" />
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageblock>
</apex:form>
</apex:page>

Preview How to Set Methods in Salesforce Apex

Salesforce Tutorial Overview


    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

4 days 24 Nov 2019

DevOps

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

Upcoming Class

5 days 25 Nov 2019

Data Science

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

Upcoming Class

5 days 25 Nov 2019

Hadoop

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

Upcoming Class

6 days 26 Nov 2019

Salesforce

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

Upcoming Class

-1 day 19 Nov 2019

Course for testing

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

Upcoming Class

34 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

13 days 03 Dec 2019

Business Analyst

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

Upcoming Class

5 days 25 Nov 2019

SQL Server

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

Upcoming Class

-1 day 19 Nov 2019

Comments

Search Posts

Reset

Receive Latest Materials and Offers on Salesforce Course

Interviews