How to write an Apex method "getFilteredContact" to find revenue > $1,000,000?
I am a Salesforce developer who is tasked with creating a custom apex controller for a particular Visualforce page that can display a list of contacts. In this task how can I write an apex method named “getFilteredContact” which can perform Database queries related to the contact and related account object to retrieve the contact belonging to the account with an annual revenue generator greater than $1,000,000?
In the context of Salesforce, here are the steps given:-
Start with a basic query
Firstly, you would need to start with a basic query creation which would select the field from the “contact” Object and include a filter on the object called “account”.
Adding the e-mail domain filter
The query should be modified if the “emailDomain” parameter is provided. It should be done to filter the contact whose e-mail address contains this domain.
Adding sorting by last name
If the “sortBylastname” parameter is true then the query should include an “ORDER BY” clause to sort the results by the last name of the contact.
Executing the query
The final query string should be executed by using the “database.query”. This method would return a list of “contact” objects which can match the criteria which is specified in the query.
Process implementation by using Java
By using Java, you would need to create the “contact” class first, which would represent a contact with properties like ID, first name, last name, etc.
Import java.util.Objects;
Public class Contact {
Private String id;
Private String firstName;
Private String lastName;
Private String email;
Private double accountAnnualRevenue;
Public Contact(String id, String firstName, String lastName, String email, double accountAnnualRevenue) {
This.id = id;
This.firstName = firstName;
This.lastName = lastName;
This.email = email;
This.accountAnnualRevenue = accountAnnualRevenue;
}
// Getters and Setters
Public String getId() {
Return id;
}
Public void setId(String id) {
This.id = id;
}
Public String getFirstName() {
Return firstName;
}
Public void setFirstName(String firstName) {
This.firstName = firstName;
}
Public String getLastName() {
Return lastName;
}
Public void setLastName(String lastName) {
This.lastName = lastName;
}
Public String getEmail() {
Return email;
}
Public void setEmail(String email) {
This.email = email;
}
Public double getAccountAnnualRevenue() {
Return accountAnnualRevenue;
}
Public void setAccountAnnualRevenue(double accountAnnualRevenue) {
This.accountAnnualRevenue = accountAnnualRevenue;
}
@Override
Public boolean equals(Object o) {
If (this == o) return true;
If (o == null || getClass() != o.getClass()) return false;
Contact contact = (Contact) o;
Return Double.compare(contact.accountAnnualRevenue, accountAnnualRevenue) == 0 &&
Objects.equals(id, contact.id) &&
Objects.equals(firstName, contact.firstName) &&
Objects.equals(lastName, contact.lastName) &&
Objects.equals(email, contact.email);
}
@Override
Public int hashCode() {
Return Objects.hash(id, firstName, lastName, email, accountAnnualRevenue);
}
@Override
Public String toString() {
Return “Contact{“ +
“id=’” + id + ‘’’ +
“, firstName=’” + firstName + ‘’’ +
“, lastName=’” + lastName + ‘’’ +
“, email=’” + email + ‘’’ +
“, accountAnnualRevenue=” + accountAnnualRevenue +
‘}’;
}
}Now you would be need to create the “contactControllerClass” which would contains the method for filtering and sorting the contact:-
Import java.util.ArrayList;
Import java.util.Collections;
Import java.util.Comparator;
Import java.util.List;
Import java.util.stream.Collectors;Public class ContactController {
Public List getFilteredContacts(List contacts, String emailDomain, Boolean sortByLastName) {
// Filter contacts by account annual revenue
List filteredContacts = contacts.stream()
.filter(contact -> contact.getAccountAnnualRevenue() > 1000000)
.collect(Collectors.toList());
// Filter by email domain if provided
If (emailDomain != null && !emailDomain.isEmpty()) {
filteredContacts = filteredContacts.stream()
.filter(contact -> contact.getEmail().contains(emailDomain))
.collect(Collectors.toList());
}
// Sort by last name if required
If (Boolean.TRUE.equals(sortByLastName)) {
Collections.sort(filteredContacts, Comparator.comparing(Contact::getLastName));
}
Return filteredContacts;
}
Public static void main(String[] args) {
// Sample data
List contacts = new ArrayList<>();
Contacts.add(new Contact(“1”, “John”, “Doe”, john.doe@example.com, 2000000));
Contacts.add(new Contact(“2”, “Jane”, “Smith”, jane.smith@domain.com, 500000));
Contacts.add(new Contact(“3”, “Emily”, “Jones”, emily.jones@example.com, 3000000));
Contacts.add(new Contact(“4”, “Michael”, “Brown”, michael.brown@sample.com, 1500000));
Contacts.add(new Contact(“5”, “Jessica”, “Johnson”, jessica.johnson@example.com, 2500000)); // Instantiate the controller
ContactController controller = new ContactController();
// Filter and sort contacts
List result = controller.getFilteredContacts(contacts, “example.com”, true);
// Print the results
Result.forEach(System.out::println);
}
}