Salesforce’s Apex properties allow developers to easily control access to an object’s characteristics in their classes. Properties are intermediates between fields and the code, allowing for more controlled value setting and retrieval. They also improve code quality and clarity, isolate business logic, and enable developers to add validation within the property itself.
What are Apex Properties?
In Salesforce Apex, properties are defined within a class by combining get and set methods. These methods are combined in a shorthand syntax to form a property. Apex properties enable encapsulation, in which data is protected from external access and managed via getter and setter methods. This encapsulation is important for protecting fields from invalid or unauthorized changes.
Here is a basic example of a property in Apex. ContactDetail is class, and contactName is property.
public class ContactDetail {
public String contactName { get; set; }
}
Types of Apex Properties
Apex properties can be of three types
1. Read-Only Properties
A read-only property can be accessed but not modified from outside the class. It only has a get function, so external code can obtain but not change the value, maintaining data consistency and preventing unintentional modifications.
public class BankAccount {
private String accountType = 'Saving'; // Private field
// Read-only property
public String getAccountType {
get { return accountType; }
}
}
2. Write-Only Properties
A write-only property in Apex allows data to be written (or set), but not read from outside the class. This indicates that we can set a value for the property, but we cannot get or read it directly. Write-only properties are less frequent than read-only or read-write properties, but they are beneficial in situations when data must be safely stored without external access for reading.
In Apex, a write-only property is defined by including just a set method and no get function:
public class AccountDataHandler {
private String secretCode;
// Write-only property
public String secretCodeProperty { set { secretCode = value; } }
}
3. Read-Write Properties:
Read-write properties commonly provide controlled access to a variable while encapsulating the logic within the class. With read-write properties, you may include validation or business logic in the setter method, ensuring that whenever a new value is set, it is validated or processed by the appropriate logic.
public class BankAccount{
private Decimal _balance;
public Decimal balance {
get { return _balance; }
set {
if (value >= 0) { // Ensuring price is non-negative
_balance= value;
} else {
throw new IllegalArgumentException('Balance cannot be negative.');
}
}
}
}
What are Automatic Properties
Automatic Properties do not require any additional code in their get or set accessor code blocks. To define an automated property, simply leave the get and set accessor code blocks empty. Automatic properties enable you to write more concise code that is simpler to debug and maintain. They can be set to read-only, read-write, or write-only.
The following example generates three automated properties.
public integer MinAge{ get; }
public double BalanceAmount{ get; set; }
public string BranchName{ set; }
What are Static Properties
When a property is marked as static, its accessor methods run in a static context. As a result, accessors do not have access to non-static member variables defined in the class.
The following example defines a class with both static and instance properties:
public class CustomerDiscount {
private static Decimal purchaseTotal = 5000;
public static Decimal discount {
get { return purchaseTotal > 1000 ? 0.1 * purchaseTotal : 0; }
}
}
How Apex Properties Benefit Developers
Apex characteristics improve development efficiency and code quality in a variety of ways.
- Encapsulation: Properties enable developers to hide a class’s implementation details, allowing them greater control over how fields are accessed and modified.
- Simplified Syntax: Instead of developing separate methods for accessing and setting values, developers can use simple property syntax. This lowers code redundancy and increases readability.
- Custom Logic: Properties enable developers to integrate validation and business logic directly into get and set operations. A setter, for example, could incorporate a validation check to guarantee that a given value falls within a certain range.
- Reduced boilerplate code: Apex properties reduce the need for explicit getter and setter methods, making the code more maintainable.
Summary
Apex properties are essential code features for Salesforce developers because they allow for succinct and maintainable code while also supporting encapsulation, validation, and business logic within the properties. They allow us to regulate how fields in a class are accessible and modified, which is particularly valuable for scenarios like data validation, controlled field exposure, and dynamic calculations.
References:
Related Posts
- How to Choose Between SOQL and SOSL Queries
- Understanding Omnistudio in Salesforce
- Salesforce Admin Jobs for Freshers
- Mastering Technical Questions for Tech Lead/Salesforce Architect Interview-4
- 20 Technical Questions to Test Your Skills in a Tech Lead/Salesforce Architect Interview-3
- Best Practices to Avoid Hardcoding in Apex for Cleaner Salesforce Code
- How to Remove Customer Details After Refreshing Salesforce Org