Methods in Salesforce in Hindi

on

|

views

and

comments

Table of Contents

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.

				
					<Access Modifier> <Return Type> <Method Name> (<Optional Parameters>) {
    // 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:

  1. Instance/Object Methods: Instance methods are associated with an instance(Object) of a class, and can be called on that instance.
  2. 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

				
					//<access modifier> class <class name>{}
//<access modifier> <access specifier>
public class Animal {
    
    // <access modifier> <DataType> <VariableName> = <Value>;
    // <access modifier> <DataType> <VariableName>;
    // 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
    // <Access Modifier> <Return Type> <Method Name> (<Optional Parameters>) {
        // 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

  1. Create an Instance method sum method with different parameters. Note: – Please use the return statement accordingly
    1. sum(1,2)
    2. sum(3,4,6)
    3. sum(3,4,6,4)
    4. sum(3,4,6,5,6)
    5. sum(3,4,6,7,8,8)
    6. sum(3,4,6…)
    7. sum(3,4,6..)
    8. sum(3,4,6)
    9. sum(3,4,6)
    10. sum(3,4,6)
    11. sum(3,4,6)
    12. sum(3,4,6)
  2. Create Class/Static methods for multiple/sub/div.
    1. Sub() – 2, 3, 4, 5, ——- up to
    2. mul() – 2, 3,4 ——
    3. div(23, 45)
    4. module() – 29/25 ~= 4

Full Video

Resources

Amit Singh
Amit Singhhttps://www.pantherschools.com/
Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion
Share this

Leave a review

Excellent

SUBSCRIBE-US

Book a 1:1 Call

Must-read

How to start your AI Journey?

Table of Contents Introduction Are you tired of the same old world? Do you dream of painting landscapes with your code, composing symphonies with data, or...

The Secret Weapon: Prompt Engineering: 🪄

Table of Contents Introduction Crafting the perfect prompt is like whispering secret instructions to your AI muse. But there's no one-size-fits-all approach! Exploring different types of...

How to Singup for your own Production org?

Let's see how we can have our salesforce enterprise Salesforce Org for a 30-day Trial and use it as a Production environment. With the...

Recent articles

More like this

LEAVE A REPLY

Please enter your comment!
Please enter your name here

5/5

Stuck in coding limbo?

Our courses unlock your tech potential