Posts

@TestVisible Annotation in Apex

In Salesforce, the @TestVisible annotation makes private or protected variables and methods visible in test classes. This annotation allows you to access and manipulate these elements for testing purposes without changing their access modifiers, thereby maintaining encapsulation and security in production code while enabling thorough testing. Here is an example  public class MyClass {     // Private member variable     @TestVisible private Integer myPrivateVariable = 09; } @isTest  private class TestVisibleClassTest {     @isTest static void testExample() {         // Access private variable annotated with TestVisible         Integer i = MyClass. myPrivateVariable;         System.assertEquals(09, i);     }  } Here are the key points about the @TestVisible annotation: Visibility : @TestVisible makes private or protected variables, properties, and methods visible in test...

@testSetup annotation in Apex

In Salesforce, the @ testSetup annotation is used in Apex tests to create test data that can be reused across multiple test methods within the same test class. This annotation is especially useful for making common test data required for various test methods, reducing redundancy, and improving test efficiency. Here's an example of how you can use the @ testSetup method in Salesforce: @isTest private class MyTestClass {          // Define test data setup method     @testSetup     static void setupTestData() {         // Create test records         Account acc = new Account(Name='Test Account');         insert acc;                  Contact con = new Contact(LastName='Test Contact', AccountId=acc.Id);         insert con;                  // You can create more test records as ...

Dynamically Calling JavaScript Functions

Dynamically invoking JavaScript functions enhances code flexibility and efficiency. By dynamically calling JavaScript functions, we can create more flexible and efficient applications that adapt to various scenarios. Dynamic Functionality : Dynamically calling JavaScript functions allows your code to adapt to different scenarios and conditions at runtime. This flexibility lets you execute functions based on user interactions, system events, or data-driven logic. Code Reusability : By dynamically invoking JavaScript functions, you can reuse standard functions across multiple components or modules within your application. This promotes code modularity, reduces duplication, and leads to cleaner and more maintainable code. Conditional Logic: Dynamically invoking functions allows you to apply conditional logic to determine which functions to execute based on specific conditions or criteria. This enables you to build more intelligent and adaptive applications that respond dynamically to cha...

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...