Salesforce Apex Tutorial Chapter 13: Apex Methods

Salesforce Apex Tutorial Chapter 13: Apex Methods

On November 4, 2021, Posted by , In Salesforce Apex Tutorial, With Comments Off on Salesforce Apex Tutorial Chapter 13: Apex Methods
Apex-Methods
Defining a method requires the following:

Access Modifier: We can use the modifiers such as protected, public, private for the method if it is required.

Data Type: It is important to specify the data type of the values returned by the method. The “Void” data type can be used if the method returns no value.

Input Parameters: Parameters are optional with methods. If the method does not have any parameter then we use a set of empty parentheses. A method can have a maximum of 32 input parameters. An input parameter list, separated by commas and followed by the type of input.

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

Syntax:

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

Example:

/** 
*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';
listToUpdate.add(con);
}

Comments are closed.