Utility Class to Get Object Metadata - A Comprehensive Guide
Introduction
In Salesforce development, Utility Classes play a crucial role in centralizing and organizing commonly used code logic, making the codebase more maintainable, readable, and modular. These custom Apex classes contain reusable methods that perform various tasks, such as data manipulation, formatting, validation, error handling, and date operations, among others. In this blog post, we will explore the concept of Utility Classes and demonstrate how to create a powerful Utility Class to fetch data from Salesforce objects dynamically.
Understanding the Utility Class: A Utility Class is an Apex class that encapsulates reusable functionalities, making it easier to manage complex code and reduce redundancy. It allows developers to package common operations into methods, which can be easily reused across the application. The main advantages of using Utility Classes are code organization, increased productivity, and code consistency.
Creating the SalesforceUtility Class: Let's dive into creating a practical Utility Class called SalesforceUtility. This class will have a method named GetObjectData, which accepts three parameters: objectName (API name of the object), fieldNames (list of API names of fields to retrieve), and clause (an optional condition for filtering data). The method will return a list of SObjects matching the specified criteria.
public class SalesforceUtility { public static List<SObject> GetObjectData(String objectName, List<String> fieldNames, String clause) { if (String.isBlank(objectName) || fieldNames == null || fieldNames.isEmpty()) { System.debug('Object name and field names are required.'); return new List<SObject>(); } String fields = String.join(fieldNames, ','); String query = 'SELECT ' + fields + ' FROM ' + objectName; if (!String.isBlank(clause)) { query += ' WHERE ' + clause; } List<SObject> records = new List<SObject>(); try { records = Database.query(query); } catch (Exception e) { System.debug('Error executing the query: ' + e.getMessage()); } return records; } }
Using the SalesforceUtility Class: Now that we have created the SalesforceUtility class, let's see how we can use it to fetch data from the Account object where the CreatedDate is earlier than today.
Don't forget to check out: Unpacking Salesforce Experience Cloud: A Comprehensive Analysis
try { String objectName = 'Account'; List<String> fieldNames = new List<String>{'Name', 'Industry'}; String queryClause = 'CreatedDate < TODAY'; List<SObject> accountData = SalesforceUtility.GetObjectData(objectName, fieldNames, queryClause); for (SObject record : accountData) { String name = (String)record.get('Name'); String industry = (String)record.get('Industry'); System.debug('Name: ' + name + ', Industry: ' + industry); } } catch (Exception e) { System.debug('Error: ' + e.getMessage()); }
Conclusion: In this blog post, we explored the concept of Utility Classes in Salesforce and how they can improve code organization and reusability. We created a powerful Utility Class, SalesforceUtility, which can dynamically fetch data from Salesforce objects based on specified criteria. By leveraging Utility Classes, developers can streamline their code and enhance productivity, resulting in more efficient and maintainable Salesforce applications.
Check out another amazing blog by Rishabh Shukla here: Send Survey Email On Case Closure on Case without using Trigger and Apex Class
With this newfound knowledge, you can take your Salesforce development skills to the next level by effectively utilizing Utility Classes in your projects. Happy coding!
Responses