Global keyword – Classes, Interfaces, and Methods – Data Manipulation Language (DML) – Salesforce Certified Platform Developer I Study Guide

Global keyword is used in Apex to define a class or method as accessible outside of the package. A Global class can be accessed by other classes in different namespaces or by external systems, such as web services. The Global class is commonly used in the following scenarios:

•\ Creating Web Services: Apex classes with the Global access modifier can be exposed as web services, allowing external systems to invoke their methods and exchange data with Salesforce.

•\ Integration with External Systems: By defining a Global class, you can establish a connection between Salesforce and external systems, enabling the exchange of data and functionality.

•\ Managed Packages: If you are developing a managed package, you can use Global classes to expose specific methods or functionality to other organizations that install your package.

Here’s a simple example:

Step 1: Open Developer Console ➤ File ➤ New ➤ Apex class ➤ insert the code

global class MyManagedPackageClass {

  • A method that is exposed for use by organizations installing the managed package

global static String processData(String input) {

  • Some logic to process the input data

return ‘Processed data: ‘ + input;

}

// Another method that is exposed

global static Integer calculateSum(Integer a, Integer b) {

  • Some logic to calculate the sum return a + b;

}

}

Step 2: Save the new class

Step 3: Open the anonymous window

Step 4: Execute the global class

// Call global methods from the managed package in the anonymous window

String result = MyManagedPackageClass.processData(‘Input data’); Integer sum = MyManagedPackageClass.calculateSum(3, 5);

System.debug(‘Result: ‘ + result);

System.debug(‘Sum: ‘ + sum);

After packaging and distributing your managed package, other organizations can use these global methods in their Apex code. This allows you to provide specific functionality to organizations that have installed your managed package while encapsulating other parts of your code to maintain a level of control and encapsulation.

Public class is a class that can be accessed by other classes and triggers within the same Salesforce org. It is the default access modifier for classes in Apex, meaning that if no access modifier is specified, the class is automatically considered public. Common use cases for public classes are

•\ Controller Classes: Public classes are often used as controller classes in the Model-View-Controller (MVC) architecture in Salesforce. These classes handle the logic and data manipulation for Visualforce pages or Lightning web components.

•\ Batch Apex: Public classes are used to define batch Apex jobs, which allow for the processing of large datasets in smaller chunks. Batch Apex classes implement the Database.Batchable interface and provide methods for processing records in batches.

•\ REST Callouts: Public classes can be used to make HTTP callouts to external services or APIs. These classes implement the HttpCalloutMock interface and provide methods for sending and receiving data over HTTP.

•\ Test Classes: Public classes are also used to write test classes for unit testing Apex code. Test classes are responsible for verifying the behavior and functionality of Apex classes and methods.

In this example, the public class serves as a simple controller:

public class SimpleControllerExample {

public String greeting { get; set; }

public SimpleControllerExample() {

greeting = ‘Hello, Salesforce!’;

}

public void updateGreeting(String newGreeting) { greeting = newGreeting;

}

}

  • Instantiate the controller and test its methods SimpleControllerExample myController = new SimpleControllerExample();
  • Print the initial greeting

System.debug(‘Initial Greeting: ‘ + myController.greeting);

  • Update the greeting myController.updateGreeting(‘New Greeting’);
  • Print the updated greeting

System.debug(‘Updated Greeting: ‘ + myController.greeting);

This code demonstrates the basic concepts of a controller class, even though it’s not connected to a user interface. It’s a simple way to showcase the functionality of a class in the anonymous window.