
Top 10 Salesforce Anti-Patterns with Examples

Introduction: The Intricacies of Anti-Patterns in Salesforce
Salesforce, as a leading Customer Relationship Management (CRM) platform, offers a multitude of features and functionalities designed to streamline business processes and enhance productivity. However, like any complex system, its implementation and customization can sometimes lead to the emergence of anti-patterns. These are common but counterproductive practices that, while initially seeming beneficial or convenient, ultimately result in inefficiencies, increased maintenance costs, or scalability issues. Understanding these anti-patterns is crucial for Salesforce developers and administrators to ensure that their solutions remain robust, efficient, and scalable.
Delving into the intricacies of anti-patterns in Salesforce involves recognizing and avoiding practices that can undermine the platform’s performance and reliability. This includes improper data modeling, over-reliance on custom code instead of leveraging native functionalities, and neglecting to follow best practices for security and data integrity. By identifying these pitfalls, professionals can adopt better practices that enhance the system’s efficiency and sustainability. Consequently, addressing these anti-patterns not only improves the immediate performance of Salesforce implementations but also ensures long-term maintainability and adaptability in the ever-evolving business landscape.
Deep Dive into Salesforce Anti-Patterns with Real-Life Examples
1. Hard-Coding Values
- Example: Embedding user IDs, record IDs, or URLs directly in Apex code or formulas.
- Issue: Hard-coded values can break when changes occur, such as user role updates or URL structure changes.
- Solution: Use custom settings, custom metadata, or environment variables to manage values dynamically.
Example of Hard-Coding Values (Anti-Pattern)
Hard-Coded User ID in Apex Trigger:
trigger OpportunityTrigger on Opportunity (before insert) {
for (Opportunity opp : Trigger.new) {
// Hard-coded User ID
opp.OwnerId = '0051U000007yT3b';
}
}
Hard-Coded URL in Visualforce Page:
<apex:page>
<apex:form>
<!-- Hard-coded URL -->
<apex:commandButton value="Go to Account" action="{!URLFOR('/0011U00000k8P3B')}" />
</apex:form>
</apex:page>
Better Practice Using Custom Settings
Custom Setting Definition:
- Go to Setup -> Custom Settings.
- Create a new Custom Setting named
ConfigSettings
. - Add a field named
DefaultOwnerId
(Text) andAccountRedirectUrl
(Text).
Custom Setting Data:
- DefaultOwnerId:
0051U000007yT3b
- AccountRedirectUrl:
/0011U00000k8P3B
Apex Trigger Using Custom Settings:
trigger OpportunityTrigger on Opportunity (before insert) {
// Fetch the custom setting value
ConfigSettings__c config = ConfigSettings__c.getInstance('Default');
String defaultOwnerId = config.DefaultOwnerId__c;
for (Opportunity opp : Trigger.new) {
opp.OwnerId = defaultOwnerId;
}
}
Visualforce Page Using Custom Settings:
<apex:page controller="RedirectController">
<apex:form>
<apex:commandButton value="Go to Account" action="{!redirectToAccount}" />
</apex:form>
</apex:page>
Apex Controller for Visualforce Page:
public class RedirectController {
public PageReference redirectToAccount() {
// Fetch the custom setting value
ConfigSettings__c config = ConfigSettings__c.getInstance('Default');
String accountRedirectUrl = config.AccountRedirectUrl__c;
PageReference pageRef = new PageReference(accountRedirectUrl);
pageRef.setRedirect(true);
return pageRef;
}
}
Better Practice Using Custom Metadata Types
Custom Metadata Type Definition:
- Go to Setup -> Custom Metadata Types.
- Create a new Custom Metadata Type named
ConfigMetadata
. - Add fields
DefaultOwnerId
(Text) andAccountRedirectUrl
(Text).
Custom Metadata Records:
- DefaultOwnerId:
0051U000007yT3b
- AccountRedirectUrl:
/0011U00000k8P3B
Apex Trigger Using Custom Metadata:
trigger OpportunityTrigger on Opportunity (before insert) {
// Fetch the custom metadata value
ConfigMetadata__mdt config = [SELECT DefaultOwnerId__c FROM ConfigMetadata__mdt WHERE DeveloperName = 'Default'];
for (Opportunity opp : Trigger.new) {
opp.OwnerId = config.DefaultOwnerId__c;
}
}
Visualforce Page Using Custom Metadata:
<apex:page controller="RedirectController">
<apex:form>
<apex:commandButton value="Go to Account" action="{!redirectToAccount}" />
</apex:form>
</apex:page>
Apex Controller for Visualforce Page:
public class RedirectController {
public PageReference redirectToAccount() {
// Fetch the custom metadata value
ConfigMetadata__mdt config = [SELECT AccountRedirectUrl__c FROM ConfigMetadata__mdt WHERE DeveloperName = 'Default'];
PageReference pageRef = new PageReference(config.AccountRedirectUrl__c);
pageRef.setRedirect(true);
return pageRef;
}
}
Using custom settings or custom metadata types helps to avoid hard-coding values, making the application more maintainable and flexible to changes.
2. Ignoring Bulk Operations
- Example: Writing Apex triggers or batch jobs that handle only single records.
- Issue: This approach can lead to governor limit exceptions when processing large datasets.
- Solution: Ensure that all Apex code, especially triggers, is bulkified to handle multiple records efficiently.
3. veruse of Triggers
- Example: Creating multiple triggers on the same object, each with its own logic.
- Issue: Multiple triggers can lead to performance issues and unpredictable behavior due to order of execution.
- Solution: Use a single trigger per object and delegate logic to handler classes to maintain clarity and control.
4. Not Using Custom Metadata Types
- Example: Storing configuration data in custom objects instead of custom metadata types.
- Issue: Custom objects consume storage limits and are less efficient for configuration purposes.
- Solution: Utilize custom metadata types for storing configuration and reference data to improve performance and manageability.
5. Ignoring Governor Limits
- Example: Writing Apex code without considering Salesforce governor limits, leading to runtime exceptions.
- Issue: Governor limit violations can halt code execution and disrupt business processes.
- Solution: Design and test Apex code with governor limits in mind, using tools like the Developer Console to monitor resource usage.
6. Inadequate Test Coverage
- Example: Writing minimal unit tests that do not cover edge cases or bulk scenarios.
- Issue: Poor test coverage can result in untested code being deployed, leading to unexpected failures in production.
- Solution: Write comprehensive unit tests that cover positive and negative scenarios, including bulk and edge cases, to ensure robust code.
7. Ignoring Best Practices for Security
- Example: Not enforcing field-level security in Apex code or Visualforce pages.
- Issue: This can lead to data exposure and security vulnerabilities.
- Solution: Always enforce field-level security and CRUD (Create, Read, Update, Delete) permissions in your Apex code and user interfaces.
8. Iverly Complex Workflows and Processes
- Example: Creating multiple complex workflow rules or process builder processes for a single object.
- Issue: Overly complex automation can be difficult to maintain and troubleshoot, and may impact performance.
- Solution: Simplify workflows and processes, consider consolidating logic into a single process or using Apex for complex automation needs.
9. Not Leveraging Declarative Features
- Example: Writing custom code for functionalities that can be achieved with Salesforce’s declarative tools.
- Issue: Custom code can be more difficult to maintain and can increase the risk of errors.
- Solution: Use declarative tools like Process Builder, Flow, and validation rules where possible to minimize custom code and simplify maintenance.
10. Poor Data Model Design
- Example: Using too many master-detail relationships or not normalizing data properly.
- Issue: Poor data modeling can lead to data redundancy, performance issues, and difficulties in reporting.
- Solution: Follow best practices for data modeling, such as proper use of lookup relationships, normalizing data, and considering the impact on performance and reporting.
Explore our Salesforce training in Ameerpet to gain real-time experience from day, enroll for free demo!
For those looking for Salesforce learning, CRS Info Solutions provides an extensive Salesforce training program designed to enhance your skills and career opportunities. Explore our Salesforce training in India to gain practical, hands-on experience.
Enroll for a free demo today!