Apex Set In SalesForce

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.
 
Apex Set In SalesForce
 
Step 2
 
Now, we can create new Apex Class ApexSet and the file named as ApexSet.apxc,
 
Apex Set In SalesForce
 
Apex Set In SalesForce
 
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,
  1. Set<DataType> variablename = new Set<DataType>();  
Apex DataType please refer to the link.
 
And add the below code for creating a Tech Set,
  1. Set<String> Tech = new Set<String> {'C','C++','Java'};  
  2. system.debug('Default Technology Set : '+Tech);  
Apex Set In SalesForce
 
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,
  1. Tech.add(‘Python’);  
  2. Tech.add('C#');  
  3. 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.
  1. Set<String> Techtest=new Set<string>();  
  2. Techtest.addAll(Tech);  
  3. system.debug('Using addAll(fromSet) - Technology Set : '+Tech);  
Next Method is, remove(element) - using this method, we can delete the element from the Set
  1. Techtest.remove('C');  
  2. system.debug('Using remove(Element) - Technology Set : '+Techtest);  
Next Method is,removeAll(List|Set) - using this method, we can delete the element from the Set
  1. Tech.removeall(Techtest);  
  2. system.debug('Using removeall(List|Set) - Technology Set : '+Techtest);  
Next Method is,contains(Element) - using this method, we can check whether the given element is in Set or not. If it is there it will return true.
  1. Boolean b=Tech.contains('Java');  
  2. system.debug('Using contains(Element) - Technology Set contains Java : '+ b);  
Next Method is,size() - using this method, we can get the total number of elements in the Set.
  1. Integer s=Tech.size();  
  2. system.debug('Using size() - Technology Set Size : '+s);  
Next Method is isEmpty() - using this method, it returns true if the set is empty.
  1. Boolean e=Tech.isEmpty();  
  2. system.debug('Using isEmpty() - Technology Set Empty : '+e);  
Finally, the ApexSet.apxc class code is,
  1. public class ApexSet {  
  2.     public static void settest() {  
  3.         Set < String > Tech = new Set < String > {  
  4.             'C',  
  5.             'C++',  
  6.             'Java'  
  7.         };  
  8.         system.debug('Default Technology Set : ' + Tech);  
  9.         //add(SetElement)  
  10.         Tech.add('Python');  
  11.         Tech.add('C#');  
  12.         system.debug('Using add(SetElement) - Technology Set : ' + Tech);  
  13.         //addAll(fromSet)  
  14.         Set < String > Techtest = new Set < string > ();  
  15.         Techtest.addAll(Tech);  
  16.         system.debug('Using addAll(fromSet) - Technology Set : ' + Techtest);  
  17.         //remove(Element)  
  18.         Techtest.remove('C');  
  19.         system.debug('Using remove(Element) - Technology Set : ' + Techtest);  
  20.         //contains(Element)  
  21.         Boolean b = Tech.contains('Java');  
  22.         system.debug('Using contains(Element) - Technology Set contains Java : ' + b);  
  23.         //size()  
  24.         Integer s = Tech.size();  
  25.         system.debug('Using size() - Technology Set Size : ' + s);  
  26.         //removeAll(List|Set)  
  27.         Tech.removeall(Techtest);  
  28.         system.debug('Using removeall(List|Set) - Technology Set : ' + Techtest);  
  29.         // isEmpty();  
  30.         Boolean e = Tech.isEmpty();  
  31.         system.debug('Using isEmpty() - Technology Set Empty : ' + e);  
  32.     }  
  33. }  
Step 4
 
For debugging the Apex class ApexSet.
 
Click the Debug menu and select Open Execute Anonymous Window.
 
Apex Set In SalesForce
 
Now, write the Apex code for calling Settest()method, and enable the Open log option for viewing the output and click "Execute".
 
Apex Set In SalesForce
 
  1. ApexSet.Settest();  
 
Step 4
 
Now, we can verify the output. After clicking "Execute", the log will open automatically and select Debug Only option.
 
Apex Set In SalesForce
 

Summary

 
Now, you have successfully tested the Apex Set and its methods in a SalesForce environment.


Similar Articles