Table of Contents
Previous Sessions
If you missed the previous sessions, please use the below links for the previous sessions
Methods(Behaviours) in Salesforce
In Salesforce, a method is a block of code that performs a specific task or action. Methods can be used to perform calculations, manipulate data, and interact with the Salesforce platform, such as by creating, updating, or deleting records.
() {
// Method Body
}
public static Integer calculateSum(Integer a, Integer b) {
return a + b;
}
// void
Types of Methods in Salesforce
There are two types of methods in Salesforce:
- Instance/Object Methods: Instance methods are associated with an instance(Object) of a class, and can be called on that instance.
- Static(Class) Methods: Static methods are not associated with an instance of a class, and can be called on the class itself. Static methods are used to perform actions that do not require an instance of the class.
Constants
In Salesforce, you can define constants using the “final” keyword in Apex code. Constants are variables whose values cannot be changed once they are initialized.
public class MyConstants {
public static final Integer PI_VALUE = 3.14;
public static final String DEFAULT_NAME = 'John Doe';
}
Hands-On Code
// class {}
//
public class Animal {
// = ;
// ;
// by default access modifier will be private for any variable
public String name = 'Max'; // String will always be withing Single Quotes
//public Id recordId = 'Max683hghyerthg'; // ( 15 digit OR 18 Digit ) 0012w00001Pz63PAAR
String accountId = '0012w00001Pz63PAAR'; // accountId ~= Id
Integer age = 5;
// declare & initialize
public static final DECIMAL PI_VALUE = 3.144;
// declare
public static final DECIMAL GRAVITY_VALUE;
// Instance Variable OR Object Variable
//Multiple-time memory allocation depends upon no objects
// Class Variable
public static String address = '123 Main Street'; // One time memory allocation
// Instance/Object Methods
// () {
// Method Body
//}
public void helloWorld(){
System.debug(' Welcome to the world of Salesforce! ');
System.debug(' Name of the Dog is '+name);
System.debug(age);
}
// Without parameters
public void speak(){
// Animal dog = new Animal();
add(10, 20);
add('Amit', 'Singh');
System.debug(name+' is Speaking'); // Max/Lucy is Speaking
}
// Method Name can be the same
// but parameter different OR parameter data types are different OR no of parameters can be different
public void add(String str, String str1){
System.debug(str+' '+str1);
}
public Integer add(Integer a, Integer b, Integer c){
return a + b + c; //
}
public String sayHello(String name){ // parameters
// ; (to terminate the statement/line)
String newAdress = '123 Main Street'; // Method Variables
System.debug(' '+ newAdress);
return 'Hello '+name;
// this is a comment
/*
* This is a comment
*/
}
// Paramatarised Methods (With Parameters)
public void add(Integer number1, Integer number2) {
Integer sum = number1 + number2;
//System.debug('Sum of the '+number1+' and '+number2+' is '+ sum);
return;
// we can not return value but we can use the return statement
}
// ClassName.Method name
// Animal.greet();
public static void greet(){
System.debug('Hello there!');
}
}
// Hands-On Code
Animal dog = new Animal();
String greeting = dog.sayHello('John');
System.debug(' '+ greeting);
Integer sum = dog.add(25,85,568); // arguments
System.debug(sum);
Animal.greet();
Animal..add(25,85,568);
Animal.GRAVITY_VALUE = 3.14;
System.debug(' '+ Animal.GRAVITY_VALUE);
Animal.PI_VALUE = 3.14;
Assignment
- Create an Instance method sum method with different parameters. Note: – Please use the return statement accordingly
- sum(1,2)
- sum(3,4,6)
- sum(3,4,6,4)
- sum(3,4,6,5,6)
- sum(3,4,6,7,8,8)
- sum(3,4,6…)
- sum(3,4,6..)
- sum(3,4,6)
- sum(3,4,6)
- sum(3,4,6)
- sum(3,4,6)
- sum(3,4,6)
- Create Class/Static methods for multiple/sub/div.
- Sub() – 2, 3, 4, 5, ——- up to
- mul() – 2, 3,4 ——
- div(23, 45)
- module() – 29/25 ~= 4