Schema Classes and Methods – Data Manipulation Language (DML) – Salesforce Certified Platform Developer I Study Guide

Override Method

Apex also supports overriding from method, which allows a child class to provide a different implementation for a method that is already implemented in its parent class. When a child class overrides a method, it provides its own implementation of

the method, which is used instead of the implementation in the parent class when the method is called on an instance of the child class. But static methods are associated with the class itself and not with instances of the class. Therefore, they cannot be overridden by child classes.

Let’s say we have a parent class called Vehicle with a method called startEngine(). The startEngine() method in the Vehicle class has a default implementation that simply outputs “Engine started.”

Step 1: Create the class vehicle and save

public virtual class Vehicle {

public virtual void startEngine() {

System.debug(‘Engine started.’);

}

}

Step 2: Create the class car and save

public class Car extends Vehicle {

public override void startEngine() { System.debug(‘Car engine started.’); System.debug(‘Step on the gas pedal.’);

}

}

Step 3: Open the anonymous window

Step 4: Create the new instance of the car

Car car = new Car();

car.startEngine();

The Vehicle class serves as the parent class and contains a method called startEngine(). This method has a default implementation that outputs “Engine started.”

The Car class is a child class that extends the Vehicle class. It overrides the startEngine() method from the parent class to provide a different implementation specific to a car. In this case, it outputs “Car engine started.” and “Step on the gas pedal.”

This demonstrates how the override keyword in Apex allows us to override methods from parent classes and provide custom implementations in child classes.

5.3  Schema Classes and Methods

The Schema class in Apex is a powerful class that allows developers to retrieve metadata information about objects, fields, and record types in Salesforce. It is part of the System namespace and provides methods to dynamically access and manipulate metadata at runtime.

Here are some common use cases for the Schema class:

•\ Object and Field Metadata: You can use the “Schema” class to retrieve information about objects and their fields, such as the object label, API name, data type, picklist values, field accessibility, and more. This can be useful when you need to perform dynamic logic based on the metadata of an object or its fields.

•\ Record Type Information: The “Schema” class allows you to access record type information for objects. You can retrieve details about record types, such as their label, developer name, ID, and whether they are active or not. This information can be used to customize behavior or display different layouts based on the record type.

•\ Dynamic Queries and DML Operations: The “Schema” class enables you to dynamically construct queries and DML operations based on the metadata of objects and fields. For example, you can dynamically build a query to retrieve all fields of an object or dynamically create a record based on the fields available on an object.

•\ Integration and Callouts: When integrating with external systems or making callouts, the “Schema” class can be used to generate the appropriate XML or JSON request payloads based on the metadata of objects and fields. This ensures that the data sent to external systems is in the correct format.

•\ Validation and Error Handling: By using the “Schema” class, you can perform validation checks on user input or data before performing operations. You can verify field lengths, data types, picklist values, and more to ensure data integrity and provide meaningful error messages to users.

Here’s an example of how you can use the “Schema” classes in Apex to dynamically retrieve information about objects and record types in Salesforce:

  • Get the object describe for the Account object Schema.DescribeSObjectResult accountDescribe = Account.sObjectType.getDescribe();
  • Get the object describe for the Contact object Schema.DescribeSObjectResult contactDescribe = Contact.sObjectType.getDescribe();
  • Get the object describe for the Opportunity object Schema.DescribeSObjectResult opportunityDescribe = Opportunity.sObjectType.getDescribe();
  • Retrieve the object label, API name, and record type
  • information for each object

String accountLabel = accountDescribe.getLabel(); String accountApiName = accountDescribe.getName(); Map<String, Schema.RecordTypeInfo> accountRecordTypes = accountDescribe.getRecordTypeInfosByName();

String contactLabel = contactDescribe.getLabel(); String contactApiName = contactDescribe.getName(); Map<String, Schema.RecordTypeInfo> contactRecordTypes = contactDescribe.getRecordTypeInfosByName();

String opportunityLabel = opportunityDescribe.getLabel(); String opportunityApiName = opportunityDescribe.getName(); Map<String, Schema.RecordTypeInfo> opportunityRecordTypes = opportunityDescribe.getRecordTypeInfosByName();

  • Print the retrieved information System.debug(‘Account Object – Label:’ + accountLabel + ‘API Name:’ + accountApiName);

System.debug(‘Account Record Types:’ + accountRecordTypes.keySet());

System.debug(‘Contact Object – Label:’ + contactLabel + ‘API Name:’ + contactApiName);

System.debug(‘Contact Record Types:’ + contactRecordTypes.keySet());

System.debug(‘Opportunity Object – Label:’ + opportunityLabel + ‘API Name:’ + opportunityApiName); System.debug(‘Opportunity Record Types:’ + opportunityRecordTypes.keySet());

In this example, we use the getDescribe() method on the sObject type to retrieve the DescribeSObjectResult object for each object (Account, Contact, and Opportunity). From the DescribeSObjectResult object, we can access various properties such as the object label, API name, and record type information using the getLabel(), getName(), and getRecordTypeInfosByName() methods, respectively.

The getRecordTypeInfosByName() method returns a map of record type names to RecordTypeInfo objects, which contain information about each record type, such as its label, API name, and whether it is available or not.

Please note that the Schema class is subject to Salesforce platform limits and permissions. You should ensure that your code has appropriate access and permissions to retrieve and manipulate metadata information.