Methods in Apex

There are two types of methods in Apex:

  • User-defined: Methods that we write in our code. For example, a person class could have “sayHello,” “speak” or “setAge” methods.
  • Standard: Methods that are provided by Salesforce. For example, String methods or Date methods.

Declaring a method

The syntax to declare an Apex method is:

public class MyClass {
  public ReturnType methodName() {
     // here you can write your code
     return ReturnValue;
  }
}

Here,

  • public: This means that you can use your method in any application within your org.
  • methodName: An identifier that you will use to call your method and execute logic within the method.
  • ReturnType: Any data type that your method returns. See the return statement section.
  • ReturnValue: Value that is returned from the method back to the calling function.

In contrast to classes, you can do a lot more things within a method:

  • Write control structures
  • Call other methods
  • Instantiate other classes
  • Create variables
  • Write comments

But you can’t write methods or create classes within methods. A method is solely made for the execution of the logic.

First method

Let’s actually create a meaningful method for our Person class. For example, a person can say their name:

public class Person {
  public String name;
  public Integer age;

  public void sayName() {
    System.debug('My name is ' + name);
  }
}

Note that we’re using the “name” variable without any dot notation. There’s no person1.name because we have access to all variables in the class within the method.

Calling methods

You can call your method from two locations:

  • From a variable.
  • From methods within the same class.

Calling methods from variables

Let’s navigate to the developer console and create a Person object. Then we’ll call the method:

Person person1 = new Person();
person1.name = 'Alex';
person1.age = 36;
person1.sayName();

In the debug logs, we can see that the name was successfully printed:

My name is Alex and I am 36

Note that we call the method on the variable—not on the class. Why? Because the method will deliver a different result for each variable depending on the value that is saved into the name variable. Here’s what I mean:

Person person1 = new Person();
person1.name = 'Alex';
person1.age = 36;
person1.sayName(); // My name is Alex

Person p2 = new Person();
p2.name = 'John';
p2.age = 18;
p2.sayName(); // My name is John

Calling methods from other methods

The second place from which we can call methods are other methods within the same class. It’s important to note that this is only possible from the same class. There is a way around this with static methods, but let’s not discuss that for now. Instead, let’s pretend that we can only call methods from other methods in the same class.

Let’s add another method named “sayAge”:

public class Person {

  public String name;
  public Integer age;

  public void sayName() {
    System.debug('My name is ' + name);
  }

  public void sayAge() {
    System.debug('I am ' + age);
  }
}

If we want to display a person’s age and name, we need to call both methods:

Person person1 = new Person();
person1.name = 'Alex';
person1.age = 36;
person1.sayName(); // My name is Alex
person1.sayAge(); // I am 36

Of course, we can pack both methods into a separate third method!

public class Person {
  public String name;
  public Integer age;

  public void sayName() {
    System.debug('My name is ' + name);
  }

  public void sayAge() {
    System.debug('I am ' + age);
  }

  public void sayNameAndAge() {
    sayName();
    sayAge();
  }
}

Now, to show the variable’s name and age, we can call just one method:

Person person1 = new Person();
person1.name = 'Alex';
person1.age = 36;
person1.sayNameAndAge(); 

The output will be:

My name is Alex

I am 36

This illustration will help you memorize how it works:

Methods in Apex
Methods in Apex

Return types

A return type is what the method gives back to the calling function. It can be any data type, String, Date, Id or even Person. For example:

public class Person {

  public Integer giveBackFive() {
    return 5;
  }

  public String returnAlex() {
    Sting igorName = 'Alex';
    return igorName;
  }

  public Person someMethodName() {
    Person p1 = new Person();

    p1.name = 'Alex';
    p1.age = 36;

    return p1;
  }
}

A method can be seen as a subflow if we make an analogy with flows in Salesforce. In this analogy, the return type would be the output type.

basics of apex merged 15

The “return” statement

The “return” keyword signals to Apex when it should stop the execution of the current method and go back to the function that called the method. The value that is returned always has the same type as the return data type from the method’s definition. A return statement is mandatory on all non-void methods.

Here are a few examples of how you can return the value “6”:

public Integer getSix() {
  return 6;
}

public Integer getSix() {
  Integer sixVariable = 6;
  return sixVariable;
}

In both cases, you will return “6” from the method. But in the first case, you return the value directly, whereas you first save the value into the variable and then return it in the second. While there’s no difference between these approaches, it’s important that the variable or value you return is of the same type as the method’s return type. In this case, that would be “Integer.”

We can’t have any logic after the return statement either. You won’t even be able to save the code if you have anything after the return statement. Apex’s compiler is smart enough to see that the execution of the current function will be stopped immediately on the return statement and that nothing will be executed after it.

basics of apex merged 17

Note that in a void method, you can also have a return statement that doesn’t return anything:

public void sayName() {
  System.debug('Alex');
  return;
}

In this example, there’s no benefit. By combining it with control structures, however, we can improve the execution time. Also, it’s worth mentioning that in many programming languages the return statement is mandatory for each method—even if it’s a void method!

Example #1 – Calculate years until retirement

Let’s add a method to our Person class in order to calculate the number of years this person must work until retirement. The methods that we wrote earlier are not displayed to save space:

public class Person {
  public String name;
  public Integer age;

  public void calculateYears() {
    Integer years = 65 - age;
    System.debug('Years until retirement: ' + years);
  }
}

Now let’s call the class from the anonymous window:

Person person1 = new Person();
person1.name = 'Alex';
person1.age = 36;
person1.calculateYears(); // Years until retirement: 29

We’re just printing out the value into the debug log. We have no access to it in the anonymous window. Now, what if we want to save the value into the variable and then work with this variable later in the anonymous window? Obviously, we need to return the number of years back to the anonymous window. 

Let’s do it:

public class Person {
  public String name;
  public Integer age;

  public Integer calculateYears() {
    Integer years = 65 - age;
    return years;
  }
}

Note that we can also write:

public class Person {
  public String name;
  public Integer age;

  public Integer calculateYears() {
    return 65 - age;
  }
}

If we execute the same code from the anonymous window…nothing happens:

Person person1 = new Person();
person1.name = 'Alex';
person1.age = 36;
person1.calculateYears(); 

That’s because we didn’t save the returned value. Let’s save it into the integer value:

Person person1 = new Person();
person1.name = 'Alex';
person1.age = 36;
Integer years = person1.calculateYears(); // save the value into a new variable
System.debug('Years until retirement: ' + years);  // Years until retirement: 29
basics of apex merged 19

The “void” return type

We already know a lot of data types in Apex, but we haven’t seen “void” so far. “void” is a unique data type that can only be found in one place: methods. We’ll never use or see this keyword anywhere else. 

So what does it mean? “void” just indicates that there’s no return type in the method. It’s used for methods that run some logic but don’t need to give back any information to the calling function. Quite often, a real-world use case is to update the opportunity stage to the next value. The method will just perform an update and return nothing.

Arguments

With the return type, we know how to get the value back from the method. But there should be a way of getting something into the method, right? Yes, meet arguments! Sometimes they are called parameters as well. These terms are completely interchangeable.

Arguments are written within round brackets (e.g. “()”). If there’s nothing inside them, it means there are no arguments. Arguments have the following syntax:

public void methodName(DataType variable1) {
    // method body
}

Here’s a real example:

public void setName(String userName) {
    // method body
}

In the example above, we have one variable. In theory, we can have up to 32 parameters. Of course, I won’t give an example of what a method with 32 parameters looks like, but you can write it yourself ;). Also, in the real world, it’s a best practice not to have more than four parameters per method.

Here’s an example with multiple parameters:

public void setNameAndAge(String userName, Integer userAge) {
    // method body
}

Example #2 – Arguments

Let’s extend our Person class with methods that have multiple arguments. As always, all the other methods are cut out from the example to save space, but you can leave them in your class:

Let’s extend our Person class with methods that have multiple arguments. As always, all the other methods are cut out from the example to save space, but you can leave them in your class:

public class Person {

  public String name;
  public Integer age;

  public void setNameAndAge(String userName, Integer userAge) {
    name = userName;
    age = userAge;
  }
}

Here’s how we can call this method in the anonymous window:

Person person1 = new Person();
person1.setNameAndAge('Alex', 32);

See how much space and code we can save with this method? We can write it like this as well:

String name = 'Alex';
Integer age = 32;
Person person1 = new Person();
person1.setNameAndAge(name, age);

Arguments in the method and the variables we use to pass the value can have completely different names. Here’s an example:

public class Person {
  public String name;
  public Integer age;

  public void setNameAndAge(String abc, Integer dddddd) {
    name = abc;
    age = dddddd;
  }
}

And the anonymous window:

String s = 'Alex';
Integer i = 32;
Person person1 = new Person();
person1.setNameAndAge(s, i);

The “this” keyword

Quite often, you will come across the “this” keyword in code. This keyword is used to differentiate between parameter and class variables. Let’s consider the following example:

public class Person {

  public String name;
  public Integer age;

  public void setName(String userName) {
    name = userName;
  }
}

In this case, when we assign the value to the name variable (name = userName), it’s clear that name is the class variable and userName is a parameter. What if we change our parameter’s name?

public class Person {

  public String name;
  public Integer age;

  public void setName(String name) {
    name = name;
  }
}

Now it’s no longer clear to Apex where the class variable and the parameter are. We can help Apex identify the class variable by using the “this” keyword:

public class Person {

  public String name;
  public Integer age;

  public void setName(String name) {
    this.name = name;
  }
}

That’s the only real use case for the “this” keyword. Why would we even use it then? Because code looks much cleaner this way and we can follow conventions when naming our parameters as well

Constructors

Constructors are special types of methods that are called when we instantiate a variable. There are three types of constructors in Apex:

  1. Default constructor
  2. Default constructor that we define
  3. Constructor with parameters

Default constructor

Actually, a constructor is called every time you instantiate a variable. We’ve already seen this multiple times. Here’s an example of a constructor being called:

Person person1 = new Person();

Wait, where’s the method call? You can identify any method call in Apex when you see “()” brackets. In this example, “Person()” is the method. We didn’t define this method in our class, though. It turns out there’s no need for that! When we instantiate any class—and if we haven’t explicitly defined any constructor in the code—the default constructor is called. The default constructor literally does nothing!

Default constructor that we define

You can override the constructor:

public class Person {

  public String name;
  public Integer age;

  public Person() {
    System.debug('The constructor is called!');
  }

  public void setNameAndAge(String userName, Integer userAge) {
    name = userName;
    age = userAge;
  }
}

Now, if we run the same code (Person person1 = new Person();), we’ll see the following output:

The constructor is called!

How do we define a constructor? It’s quite simple. There’s no return type and the method name is equal to the class name:

public class ClassName {

  public ClassName() {
    // constructor body
  }

}

Constructor with parameters

The best thing about constructors is that we can create several of them that accept different arguments. Here’s an example with our Person class:

public class Person {

  public String name;
  public Integer age;

  public Person() {
    System.debug('The constructor is called!');
  }

  public Person(Integer userAge) {
    age = userAge;
  }

  public Person(String userName) {
    name = userName;
  }

  public Person(Integer userAge, String UserName) {
    name = userName;
    age = userAge;
  }

  public void printPerson() {
    Syste.debug('My name: ' + name + ', my age: ' + age);
  }
}

And here are examples in the same order with constructors and outputs:

Person person1 = new Person(); // 'The constructor is called!'

Person person2 = new Person(32); 
person2.printPerson(); // My name: null, my age: 32

Person person3 = new Person('Alex'); 
person3.printPerson(); // My name: Alex, my age: null

Person person4 = new Person('Alex', 32); 
person4.printPerson(); // My name: Alex, my age: 32

Some of the values are null because we didn’t set them in the controller. Also, we now only need one line to write the same logic that required three lines at the beginning!

There’s one more thing to note about constructors. If we define any constructor but don’t define the default constructor, we’ll get an error:

public class Person {

  public String name;
  public Integer age;

  public Person(Integer userAge, String UserName) {
    name = userName;
    age = userAge;
  }
}

If you try to run this code, you’ll get an error:

Person person1 = new Person();
image 4

The reason is that the Apex compiler will start searching for the default constructor in our Person class and won’t find one. You can solve this issue by defining an empty default constructor in the class or completely removing all constructors:

public class Person {

  public String name;
  public Integer age;

  public Person() {
  }

  public Person(Integer userAge, String UserName) {
    name = userName;
    age = userAge;
  }
}

Don’t worry too much about constructors in Apex. They’re used a lot in combination with sObjects (we’ll get into those later), but you can code without using any constructors. I personally don’t like constructors and don’t use them unless it’s necessary, but that’s just a question of style.

Methods in other classes

We still haven’t discussed standard methods. These are methods that the Salesforce team wrote for each of the classes. Strings, Integers, Dates and so on are all classes in Apex. Once you have a task that you need to solve, you should look at these methods first and see if you can use them. If not, only then should you consider writing the methods on your own.

Let’s take, for example, a String method from the official documentation.

There are a lot of methods. If you click on the method, you will be navigated to the method’s description. For example, this is what we get for the “contains” method:

image 3

You can see the definition of the method, parameters and examples. However, we can’t see the method’s source code because Salesforce keeps it secret.

The end?

That was a lot, I know! I don’t expect anyone to absorb all the information in this article at once. There are still a lot more advanced topics to discuss, but you have everything you need to write meaningful Apex code in this guide.

Links

  • Salesforce Apex Master Class (Ep.12) – What is a Method in Apex? (Coding with the Force – YouTube).
  • Additional reading about constructors. You can code without them, but it’s better to understand them anyway.
  • A “static” keyword in Apex. We didn’t discuss this because it’s a more advanced topic and I don’t want to confuse you. You can read about it on your own, but you don’t really need this topic to write good Apex code. Here are some good resources:
    • Introduction to the “static” keyword (SFDC99).
    • Salesforce Apex Programming: Static vs Instance Methods (YouTube).
    • Question About Static Methods (Stack Overflow).
<h4 class="item-title">admin</h4>

admin

Related Posts

blog images 12

SOQL in Apex

blog images 11

Loops in Apex

blog images 10

Lists in Apex

Leave a Reply

Your email address will not be published. Required fields are marked *

t.me/ihor_igor

Subscribe for news about new courses or discounts.

No spam, we promise.


    © 2023 All Rights Reserved by site  igor@cloud-prism.com