Posts

Best practices for writing test classes in Salesforce

Writing effective test classes is crucial in Salesforce development to ensure the quality and stability of your code. Cover as many lines of code as possible. Testing Best Practices Code Coverage Best Practices Here are some best practices for writing test classes in Salesforce: Test Coverage: Aim for high code coverage, typically aiming for at least 75% coverage for your Apex classes, and 100% if possible. Ensure that your test methods exercise all code paths, including positive and negative scenarios. Isolation : Write tests that are isolated from each other and from the data in the organization. Use the @testSetup annotation to create test data once and reuse it across test methods, minimizing the need to insert duplicate data in each test method. Data Creation : Create the necessary test data within your test class using Apex code rather than relying on existing data in the organization. This ensures that your tests are self-contained and portable, independent of changes in th...

Asynchronous Apex in Salesforce

Image
Asynchronous Apex in Salesforce refers to executing code asynchronously, meaning that the code is executed in the background without blocking the user interface. Asynchronous Apex is typically used for long-running or resource-intensive processes that would otherwise exceed the transaction execution time limits. Queueable Apex Take control of your asynchronous Apex processes by using the Queueable interface. This interface enables you to add jobs to the queue and monitor them. Using the interface is an enhanced way of running your asynchronous Apex code compared to using future methods. Apex Scheduler Batch Apex Future Methods This table lists the asynchronous Apex features and when to use each. Here'r multiple ways for running your Apex code asynchronously. @future Annotation : Methods annotated with @future are queued for execution in a separate transaction. This allows you to perform tasks asynchronously. @future methods can accept parameters and return void, making them suitab...

Apex Triggers in Salesforce

Image
In Salesforce, a trigger is a piece of Apex code that executes before or after specific data manipulation events occur, such as inserting, updating, deleting, or undeleting records. Triggers enable you to perform custom actions or validations when these events take place on Salesforce records. Refer to the below links for more details - Get Started with Apex Triggers Apex Triggers Define Apex Triggers Here are some key points about triggers in Salesforce: Execution Context: Triggers execute in response to specific database events and run in the Salesforce environment. Supported Events : Triggers can be defined to execute before or after specific events, including insert, update, delete, undelete, and upsert. Object-Specific: Triggers are associated with specific Salesforce objects, such as Accounts, Contacts, Opportunities, etc. Each trigger operates on records of its associated object type. Apex Language: Triggers are written in Apex, Salesforce's proprietary programming langua...

Working with Field Sets

Image
A field set is a grouping of fields. For example, you could have a field set that contains fields describing a user's first name, middle name, last name, and business title. Field sets are available for Visualforce pages on API version 21.0 or above. You can have up to 50 field sets referenced on a single page. An sObject can have up to 2,000 field sets. Each field set can have up to 25 fields through lookup relationships. Fields can only span one level away from the entity. Setup Instructions Select the Account object. In the Account object detail page, go to Fields & Relationships. Click "Field Sets" and then "New". Name your field set (e.g., "Account_Details") and add the fields you want to include in the field set. Save the field set. Visualfore Page Example <apex:page standardController="Account">     <apex:form>         <apex:pageBlock title="Account Information">             <apex:pageBlockSecti...

Person Accounts in Salesforce

Image
Person Accounts in Salesforce are a special type of account designed to represent individual consumers rather than businesses or organizations. Enable Person Accounts In Salesforce, Person Accounts are represented by records with the " __pc " suffix appended to their API names. This suffix stands for "Person Account." When Person Accounts are enabled in Salesforce, they create a hybrid record type that combines elements of both the Account and Contact objects into a single record. All standard and custom fields associated with Person Accounts will have the " __pc " suffix appended to their API names. For example, if you have a custom field named " PreferredLanguage__c ," it will appear as " PreferredLanguage__pc " for Person Accounts. Siginificance of a Person Accounts in Salesforce Definition : A Person Account in Salesforce is essentially a hybrid record type that combines elements of both the Account and Contact objects. It represen...

Implement deletion of checked values in a Visualforce page

Here's a simplified example to illustrate the approach: Visualforce Page <apex:page controller="MyController">     <script>         function deleteSelectedRecords() {             var selectedIds = [];             // Loop through checkboxes and collect IDs of checked records             document.querySelectorAll('input[type="checkbox"]:checked').forEach(function(checkbox) {                 selectedIds.push(checkbox.value);             });             // Call server-side method to delete selected records             MyController.deleteRecords(selectedIds, function(result, event) {                 // Handle response from server                 if (event....

Not able to create Trigger on Attachment from Salesforce UI

Image
Salesforce doesn't have the option to allow developers to create triggers directly on standard objects like Attachment using the Salesforce UI.  Instead, we've used tools  Force.com IDE in Eclipse to write and deploy triggers on Attachment. For the Attachment, ContentDocument, and Note standard objects, you can’t create a trigger in the Salesforce user interface. For these objects, create a trigger using development tools, such as the Developer Console or the Salesforce extensions for Visual Studio Code. Alternatively, you can also use the Metadata API. Here's an example