Force.com Platform Fundamentals: Apex Classes

Ashish KothariSalesforce, Technical Tips1 Comment

Force.com Platform Fundamentals: Apex Classes

Apex Classes:

  • An apex class is a blueprint from which Objects are created
  • Classes consist of attributes and methods
  • Classes may contain other classes, known as inner classes (one level deep nested classes)
  • Static attributes and methods can only be declared in a top-level class definition
  • Exception class must be extended in case you want to create new extension classes
  • Classes can be enabled or disabled for different profiles

 

Syntax:

private | public | global
[virtual | abstract | with sharing | without sharing | (none)] Class className [implements interfaceName | (none)] [extends classOrInterfaceName | (none)] {
// Class Body
}

 

Key Points:

  • If defined global, the class is accessible everywhere. All attributes, methods and/or inner classes that are defined global must be within a global class
  • If defined public, the class is visible across the org, application or namespace that comprises the class
  • Top level or outer classes must have be either global or public
  • If an inner class is defined private, the inner class is only accessible to the outer class. The default access modifier for inner classes is private
  • A class can implement multiple interfaces, but can only extend one class
  • You can implement and extend classes using the keywords: virtual, abstract and extends
    • virtual keyword declares that the class allows extensions and overrides; classes that are virtual cannot be global
    • abstract keyword declares that the class contains abstract methods and can be extended. These classes just have a method signature and do not have code. You cannot instantiate an object of an abstract class until some other class extends it
    • extends keyword declares that the class is a subclass. The “super” keyword can be used to invoke constructors and methods from the parent class
  • Interfaces are classes that only include the method signature. The methods are not implemented in the interfaces. Apex supports both top-level and inner interfaces

 

Access Modifiers:

  • Private: Default access modifier, not available for top-level classes. It implies that the attribute or method is only available in the class where it is defined
  • Public: Can be used by an apex code in the org, application or namespace
  • Protected: Available only to inner classes
  • Global: Accessible by all Apex code everywhere. Note that Apex code cannot be shared between different org’s, with the exception of Web Services, which are accessible everywhere

 

Attributes Syntax:

accessModifier dataType attributeName initalizationValue;

e.g.
public static final Integer minAmount = 1;
 

Methods Syntax:

accessModifier returnType methodName(inputParameters){
// Method Body
}

e.g.
public Integer getInt(){
Integer intValue = 100;
return intValue;
}

 

Apex System Delivered Classes:

  • System Class – A static class that contains only static methods. e.g.
    • Debug()
    • Now()
    • Today()
    • Assert()
    • AssertEquals()
    • AssertNotEquals()
  • UserInfo Class – Contains getter methods used to retrieve details. e.g.
    • getUserId()
    • getUserName()
    • getUserRoleId()
    • getLocale()
    • getFirstName()
    • getLanguage()

Apex process classes are used to submit workflow requests and process the results of those requests. The three classes for this purpose are:

  • ProcessRequest – processes the results from a workflow process
  • ProcessSubmitRequest – submits a workflow item for approval
  • ProcessWorkItemRequest – processes an item after it is submitted

 
[message_box title=”Salesforce.com Certified” color=”blue”]From the small business to the large enterprise, we have solutions to help improve win rates.[/message_box] Contact Us

One Comment on “Force.com Platform Fundamentals: Apex Classes”

Leave a Reply

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