About Salesforce
Salesforce is a cloud based online solution for customer relationship management, or CRM. Salesforce provides all of our departments like marketing, sales, commerce, and service with a shared view of our customers with a single integrated CRM platform.
Apex is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on Salesforce servers in conjunction with calls to the API. Using syntax that looks like Java and acts like database stored procedures, Apex enables developers to add business logic to most system events, including button clicks, related record updates, and Visualforce pages. Apex code can be initiated by Web service requests and from triggers on objects. – Salesforce
Apex Collections is a type of variable, it can store multiple number of records. The different types of Collections are, List, Set and Map.
A set is an unordered collection of elements that do not contain any duplicates. Set elements can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types.
Reading this article, you will learn and test the Apex Set and Methods in SalesForce.
The prerequisites for testing Apex Set in SalesForce are:
Register a Salesforce Free Trail account using the following link.
Step 1
Login your Salesforce Account and Click the Developer Console.

Step 2
Now, we can create new Apex Class ApexSet and the file named as ApexSet.apxc,


After creating Apex class ApexSet, and add a settest() method for creating and testing the Set and its methods,
The General Syntax for Apex Set is,
- Set<DataType> variablename = new Set<DataType>();
Apex DataType please refer to the link.
And add the below code for creating a Tech Set,
- Set<String> Tech = new Set<String> {'C','C++','Java'};
- system.debug('Default Technology Set : '+Tech);

Step 3
Next, we can add the Apex Set with some predefined important methods like add(SetElement), addAll(fromList | fromSet), remove(element), removeAll(List | Set), Contains(Element), size() and isEmpty().
First Method is, add(SetElement) – using this method, we can insert an element into the Set. No duplicate value to be added. The code is,
- Tech.add(‘Python’);
- Tech.add('C#');
- system.debug('Using add(SetElement) - Technology Set : '+Tech);
Next Method is, addAll(fromList | fromSet) - using this method, we can add all the elements in the Set or List specified, and both set or list should be the same type.
- Set<String> Techtest=new Set<string>();
- Techtest.addAll(Tech);
- system.debug('Using addAll(fromSet) - Technology Set : '+Tech);
Next Method is, remove(element) - using this method, we can delete the element from the Set
- Techtest.remove('C');
- system.debug('Using remove(Element) - Technology Set : '+Techtest);




Join the conversation! Your thoughts help the community grow.