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);
Next Method is,removeAll(List|Set) - using this method, we can delete the element from the Set
- Tech.removeall(Techtest);
- 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.
- Boolean b=Tech.contains('Java');
- 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.
- Integer s=Tech.size();
- system.debug('Using size() - Technology Set Size : '+s);
Next Method is isEmpty() - using this method, it returns true if the set is empty.
- Boolean e=Tech.isEmpty();
- system.debug('Using isEmpty() - Technology Set Empty : '+e);
Finally, the ApexSet.apxc class code is,
- public class ApexSet {
- public static void settest() {
- Set < String > Tech = new Set < String > {
- 'C',
- 'C++',
- 'Java'
- };
- system.debug('Default Technology Set : ' + Tech);
-
- Tech.add('Python');
- Tech.add('C#');
- system.debug('Using add(SetElement) - Technology Set : ' + Tech);
-
- Set < String > Techtest = new Set < string > ();
- Techtest.addAll(Tech);
- system.debug('Using addAll(fromSet) - Technology Set : ' + Techtest);
-
- Techtest.remove('C');
- system.debug('Using remove(Element) - Technology Set : ' + Techtest);
-
- Boolean b = Tech.contains('Java');
- system.debug('Using contains(Element) - Technology Set contains Java : ' + b);
-
- Integer s = Tech.size();
- system.debug('Using size() - Technology Set Size : ' + s);
-
- Tech.removeall(Techtest);
- system.debug('Using removeall(List|Set) - Technology Set : ' + Techtest);
-
- Boolean e = Tech.isEmpty();
- system.debug('Using isEmpty() - Technology Set Empty : ' + e);
- }
- }
Step 4
For debugging the Apex class ApexSet.
Click the Debug menu and select Open Execute Anonymous Window.
Now, write the Apex code for calling Settest()method, and enable the Open log option for viewing the output and click "Execute".
Step 4
Now, we can verify the output. After clicking "Execute", the log will open automatically and select Debug Only option.
Summary
Now, you have successfully tested the Apex Set and its methods in a SalesForce environment.