Table of Contents
Previous Sessions
If you missed the previous sessions, please use the below links for the previous sessions
Constructor in Salesforce
A constructor is a unique method that is used to create and initialize an instance/Object of a class. When an object is created from a class, the constructor is automatically called to initialize the object’s state and properties.
- The constructor does not have any return type.
- The name of the constructor must be the same as the Class Name.
- The parent class(topmost class) constructor should always be public.
- Private in cases where the methods are class methods.
Example:-
Animal dog = new Animal();
Where -
Animal - Class
dog - the variable name of the Object
= - Assignment operator
new - keyword
Animal() - constructor
Types of Constructor
There are two types of constructors in Salesforce:
- Default Constructor: A default constructor is a constructor that is automatically created by Salesforce if no other constructor is defined in the class.
- The default constructor takes no arguments and does not perform any initialization.
- A parameterized constructor is a constructor that takes one or more parameters and uses them to initialize the object’s state and properties.
- When you want to enforce that user must need to pass the value while creating the instance/Object.
- Parameterized constructors are useful for setting initial values or default values for object properties.
- Animal dog = new Animal(‘Paremrian’);
public List stateList;
pubic StatePicker(String countryName){
stateList = {'','',''};
}
() {
// Constructor Body
}
public class MyObject {
public String name;
public String description;
public MyObject(String objectName, String objectDescription) {
this.name = objectName;
this.description = objectDescription;
}
}
Conditional Statements
- Conditional Statements
- If else
- Switch
Use the “if-else” statement in Apex code to execute different blocks of code based on a condition
if (condition) {
// code block to execute if the condition is true
} else {
// code block to execute if the condition is false
}
if (condition) { // boolean (TRUE/FALSE)
// code block to execute if the condition is true
} else if(condition) {
// code block to execute if the condition is false
} else {
}
// Example 1
Integer x = 5;
if (x > 10) { // <, = , >=, <=, != <>
System.debug('x is greater than 10');
} else {
System.debug('x is less than or equal to 10');
}
Integer x = 5;
if (x > 10) { // <, = , >=, <=, != <>
System.debug('x is greater than 10');
} else if(x >= 5) {
System.debug('x is greater than or equal to 5');
} else{
System.debug('x is less than 5');
}
// Example 2
String fruit = 'banana';
if (fruit == 'apple') {
System.debug('This is an apple');
} else if (fruit == 'orange') {
System.debug('This is an orange');
} else {
System.debug('This is neither an apple nor an orange');
}
Switch Statement in Salesforce
You can use the “switch” statement in Apex code to execute different blocks of code based on the value of a variable.
The syntax for the “switch” statement is as follows:
switch on expression {
when value1 {
// code block to execute if expression equals value1
}
when value2 {
// code block to execute if expression equals value2
}
when value3 {
// code block to execute if expression equals value3
}
when else {
// code block to execute if the expression does not equal any of the specified values
}
}
Here’s an example of how to use the “switch” statement in Apex code:
String fruit = 'banana';
switch on fruit {
when 'apple' {
System.debug('This is an apple');
}
when 'orange' {
System.debug('This is an orange');
}
when 'banana' {
System.debug('This is a banana');
}
when else {
System.debug('This is not an apple, orange, or banana');
}
}
Hands-on Code
public class Laptops {
/* Instance Variable / Object Variable */
public String companyName; // default value ~= null
public Boolean isAdult; // default value ~= null
public String processor;
public Integer ramSize;
/* Class Variable / Static Variable */
public static String name;
/*
public Laptops(){
}
*/
// Account acc = new Account(Name ='Test Account');
// Account acc = new Account(Name ='Test Account', Industry='Education');
public Laptops(Integer ramSize){
this.ramSize = ramSize;
// this.ClassVariableName =
}
public Laptops(String companyName){
System.debug('Company Name in constructor is =>'+ companyName);
this.companyName = companyName;
// this.ClassVariableName =
}
public Laptops(String companyName, String processor){
System.debug('Company Name in constructor is =>'+ companyName);
this.companyName = companyName;
this.processor = processor;
}
public Laptops(String companyName, String processor, Integer ramSize){
System.debug('Company Name in constructor is =>'+ companyName);
this.companyName = companyName;
this.processor = processor;
this.ramSize = ramSize;
}
public void listAllLaptops(){
// SOQL
}
// Instance Method / Object Method
public void printCompanyName(){
System.debug(' Company Name withing printCompanyName method is => '+ companyName);
System.debug(' Company Name withing printCompanyName method is => '+ name);
if(companyName != null){
System.debug(' Company Name withing printName method is => '+ name);
// SOQL - SELECT ID, Name, ----- FROM Laptop__c WHERE Company_Name = ''
} else if( companyName != null && processor != null ){ // && = AND, || = OR
System.debug(' companyName withing printName method is => '+ companyName);
System.debug(' processor withing printName method is => '+ processor);
// SOQL - SELECT ID, Name, ----- FROM Laptop__c WHERE Company_Name = '' AND Processor__c = ''
} else if(companyName != null && processor != null && ramSize != null){
System.debug(' companyName withing printName method is => '+ companyName);
System.debug(' processor withing printName method is => '+ processor);
System.debug(' ramSize withing printName method is => '+ ramSize);
// SOQL - SELECT ID, Name, ----- FROM Laptop__c WHERE Company_Name = '' AND Processor__c = '' AND RAM_SIZE__c = 784;
} else if(companyName != null || processor != null || ramSize != null){
//
} else {
}
}
// Class Method / Static Methods
public static void printName(){
// Static Variable/Class Variable
}
}
//Laptops apple = new Laptops('Apple');
//apple.printCompanyName();
Laptops.name = 'Apple';
Laptops.printName();
String fruit = 'bananat632';
switch on fruit {
when 'apple' {
System.debug('This is an apple');
}
when 'orange' {
System.debug('This is an orange');
}
when 'banana' {
System.debug('This is a banana');
}
when else {
System.debug('This is not an apple, orange, or banana');
}
}
Integer x = 56;
Integer y = 10;
Integer z = (x / y);// +, - , /, *, %
System.debug(' '+z);
Assignment
Create a class “Car” that should have the following
- The class should have the private variables
- Name
- Model
- Year
- Company
- Type
- Create multiple constructors that are accepting the different parameters
- Car(String name, String type)
- Car(String name)
- Car( )
- Car(String name, String model, String year, String company, Type)
- Car(Integer year)
- Within the constructors assign the values to the private variables. Note: – The value should be assigned to the variable that is passed in the constructor and for other variables assign a blank value. See the example below
- public Car(String year){
this.year = year;
this.name = ”;
this.model = ”;
this.company = ”;
this.type = ”;
}
- public Car(String year){
- Create various methods to print the Car Details
- printCarInformation( ) → This method should print all the information about the Car
- printCarName( ) → This method should only print the name of the Car
- printCarYear( ) → This method should only print the built year of the Car
- printCarNameType( ) → This method should only print the name & type of the Car
Full Video
Resources
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_constructors.htm
- https://www.tutorialkart.com/learn_apex/constructor-in-apex-programming/
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_if_else.htm
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_switch.htm
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_expressions_operators_understanding.htm
- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_SafeNavigationOperator.htm
- Math Class – https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_math.htm
- String Class – https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm