Posts

Trigger Logic with Apex Switch

Switch Statements Apex provides a switch statement that tests whether an expression matches one of several values and branches accordingly. Switch Statements Rethink Trigger Logic with Apex Switch Trigger Context Variables The context variable Trigger.operationType is used to get the current System.TriggerOperation enum value. The following are the values of the System.TriggerOperation enum: AFTER_DELETE AFTER_INSERT AFTER_UNDELETE AFTER_UPDATE BEFORE_DELETE BEFORE_INSERT BEFORE_UPDATE trigger customerTrigger on Account (before insert, after insert) { switch on Trigger.operationType { when BEFORE_INSERT { System.debug('Before Insert'); } when AFTER_INSERT { System.debug('After Insert'); } when else { System.debug('Something went wrong'); } } }

Managed vs Unmanaged in Salesforce

Image
Managed Packages in Salesforce are a way for developers to package and distribute their custom applications and components to other Salesforce organizations. These packages encapsulate custom objects, fields, Apex code, Visualforce pages, Lightning components, and other metadata components developed on the Salesforce platform. Unmanaged packages in Salesforce are collections of metadata components that are bundled together for distribution and deployment. Unlike managed packages, which provide IP protection and versioning capabilities, unmanaged packages are primarily used for sharing customizations within a single Salesforce organization or for simple deployments.  Here is a comparison between Managed and Unmanaged packages. Managed Packages: Packaging and Distribution: Managed packages are packaged and distributed by Salesforce developers or ISVs (Independent Software Vendors) through the Salesforce AppExchange or other distribution channels. Developers can package custom object...

Junction objects in Salesforce

In Salesforce, a junction object is a custom object with two master-detail relationships, typically linking two other objects together in a many-to-many relationship. Junction objects are used to model complex relationships between records in a way that cannot be represented by a direct master-detail or lookup relationship. Here's a common use case for junction objects: Use Case: Student Enrollment in Courses Consider a scenario where you have two objects: Student and Course. Each student can be enrolled in multiple courses, and each course can have multiple students enrolled. However, you also need to track additional information about each student's enrollment in a specific course, such as enrollment date, grade, and attendance. To model this scenario using junction objects, you would create three custom objects: Student : Represents individual students. Course : Represents individual courses. Enrollment (Junction Object) : Represents the many-to-many relationship between st...

Visualforce PDF Rendering Considerations and Limitations

It is essential to consider these limitations when designing Visualforce pages that will be rendered to PDF. Always verify the formatting and appearance of the PDF version of your page before putting it into production. The following are some limitations of the Visualforce PDF rendering service Limitations of the Visualforce PDF rendering service include the following. PDF is the only supported rendering service. The PDF rendering service renders PDF version 1.4 and CSS versions up to 2.1. Rendering a Visualforce page as a PDF file is intended for pages designed and optimized for print. A Visualforce page rendered as a PDF file displays either in the browser or is downloaded, depending on the browser’s settings. Specific behavior depends on the browser, version, and user settings, and is outside the control of Visualforce. The PDF rendering service renders the markup and data on your page, but it might not render formatting contained within the contents of rich text area fields added t...

"One Trigger Per Object" Pattern

The " One Trigger per Object " pattern is a best practice in Salesforce development that involves consolidating multiple trigger logic into a single, well-structured trigger handler class per object. This pattern helps to maintain clean and organized code, reduces the risk of trigger order issues, and facilitates better code reuse and maintainability. Trigger Handler Class public class AccountTriggerHandler {          public static void onBeforeInsert(List<Account> newList) {         // Your before insert logic here     }          public static void onBeforeUpdate(List<Account> newList, Map<Id, Account> oldMap) {         // Your before update logic here     }          public static void onBeforeDelete(List<Account> oldList) {         // Your before delete logic here     }        ...

System.CalloutException: Callout from triggers are currently not supported

The error message "System.CalloutException: Callout from triggers is currently not supported" indicates that a trigger in your Salesforce org is attempting to make a callout to an external system, which is not allowed. Salesforce does not allow callouts to be made directly from triggers. This restriction ensures the integrity and consistency of data in the Salesforce database. If handled improperly, callouts can result in long-running transactions, leading to performance issues or data inconsistencies. For example, when a new Account record is inserted and this trigger fires, you encounter the "System.CalloutException: Callout from triggers are currently not supported" error. trigger AccountTrigger on Account (after insert) {     for (Account acc : Trigger.new) {         // Make a callout to an external API         HttpRequest req = new HttpRequest();         req.setEndpoint('https://api.example.com');    ...

Lightning Design System (SLDS) in Visualforce pages

Salesforce Lightning Design System provides a set of CSS classes and design guidelines that allow you to create user interfaces that are consistent with the Salesforce Lightning Experience.  You can use the Lightning Design System (SLDS) to build Visualforce pages that match the look and feel of the Salesforce mobile app. To use SLDS, it takes some tweaks in your code and a few things to remember. Visualforce Page with SLDS <apex:page applyHtmlTag="false" applyBodyTag="false">     <html lang="en">     <head>         <title>SLDS Example in Visualforce</title>         <apex:slds />     </head>     <body>         <div class="slds-scope">             <div class="slds-grid slds-grid_vertical">                 <div class="slds-col slds-size_1-of-2">  ...