Methods in Salesforce Apex

Methods in Salesforce Apex

On January 20, 2024, Posted by , In Salesforce Apex Tutorial, With Comments Off on Methods in Salesforce Apex
Methods in Salesforce Apex
Methods in Salesforce Apex

Table of Contents

What is a method?

A method in programming is a block of code designed to perform a specific task, encapsulated within a class or object, which can be called and executed whenever needed, often with the ability to receive inputs (parameters) and return a result.

Access Modifier:

An access modifier is a keyword in programming languages that sets the accessibility or visibility of classes, methods, and other members. Access modifiers control where these members can be accessed from, enhancing security and encapsulation in code.

Common Access Modifiers:

  1. Public:
    • Accessible from any other class.
    • Example in Java: public int myVariable;
    • Example in Apex: public void myMethod() { }
  2. Private:
    • Accessible only within the class it is declared.
    • Example in Java: private int myVariable;
    • Example in Apex: private void myMethod() { }
  3. Protected:
    • Accessible within the class, subclasses, and classes in the same package (Java) or namespace (Apex).
    • Example in Java: protected int myVariable;
    • Example in Apex: protected void myMethod() { }
  4. Default (Package-Private) (Java-specific):
    • Accessible only within its own package.
    • Example in Java: int myVariable; (No modifier keyword)
  5. Global (Apex-specific):
    • Accessible by any Apex code in any namespace.
    • Example in Apex: global void myMethod() { }


public class ExampleClass {
    private Integer myPrivateVariable;  // Private access modifier
    public Integer myPublicVariable;    // Public access modifier

    protected void myProtectedMethod() {
        // Protected method logic

    global void myGlobalMethod() {
        // Global method logic

Data Type:

Data types specify the type of data a variable can hold, such as integers, strings, Booleans, etc.


public class MyDataTypes {
Integer myNumber = 10;
String myString = 'Hello, Salesforce!';
Boolean myBoolean = true;

Input Parameters:

Input parameters are the variables passed to a method when it is called. They allow methods to receive data to process.


public class MyParameters {
    public void myMethod(String name, Integer age) {
        System.debug('Name: ' + name);
        System.debug('Age: ' + age);

Body of Method:

Method body enclosed in braces {}. All the method code is written here, including local variable declarations.


[public | private | protected | global] [override] [static] data_type method_name 
(input parameters) 
// The body of the method


*public access modifier
*void return type
*name parameter
 Public static void updateContact(string name){   
//method body
List<Contact> conList = [Select Id,FirstName,LastName from Contact Where LastName =: name];
       List<contact> listToUpdate = new List<Contact>();
        for(contact con:conList){
            con.Title = 'Manager';

CRS Info Solutions offers a comprehensive and dynamic Salesforce online course career building program for beginners, covering admin, developer, and LWC concepts. This course features immersive real-time projects, interactive hands-on learning, detailed daily notes, essential interview questions, thorough certification preparation, and strategic job prep guidance. Join their inspiring free demo to embark on an exciting Salesforce journey with expert mentorship and unlock your full potential in the Salesforce ecosystem. Enroll for a free demo today!

Next Chapter is Objects in Salesforce Apex and previous chapter is Classes.

Comments are closed.