Posts

Ajax in Visualforce page

In Visualforce, you can use Ajax (Asynchronous JavaScript and XML) techniques to perform asynchronous requests to the server without refreshing the entire page. This allows you to update specific parts of the page dynamically, providing a smoother and more responsive user experience. Here's how you can use Ajax in Visualforce pages: Using <apex:actionFunction>: <apex:actionFunction> allows you to define a JavaScript function that invokes an Apex controller method without a full page refresh. It's typically used to perform server-side actions based on user interactions. Example <apex:page controller="MyController">     <apex:form>         <apex:commandButton value="Click Me" onclick="doAction(); return false;"/>         <apex:actionFunction name="doAction" action="{!myControllerMethod}" rerender="outputPanel"/>     </apex:form>     <apex:outputPanel id="outputPanel"...

Render a Visualforce Page as a PDF File

Image
Render a Visualforce Page as a PDF File When you render a Visualforce page as a PDF file, it can either be displayed in the browser or downloaded depending on the user's settings. The behavior may vary based on the browser, its version, and user preferences, which is beyond Visualforce's control. You can use the PDF rendering service to generate a printable PDF of a Visualforce page. Example - Visualforce Page <apex:page standardController="Contact" renderAs="pdf"> <h1>Contact Details - Hi {!Contact.Name}</h1> <p>Your account details are:</p> <table> <tr><th>Account Name</th> <td><apex:outputText value="{!Contact.Account.Name}"/></td> </tr> <tr><th>Customer Since</th> <td><apex:outputText value="{0,date,long}"> <apex:param value="{!Contact.CreatedDate}"/> </apex:outputText></t...

Process Builder Bulkified

To create a bulkified process in Process Builder using an Apex class, we'll need to ensure that the Apex class and the invocable method within it are capable of handling bulk records efficiently. Example public class MyBulkifiedClass {     @InvocableMethod(label='Process Bulk Records' description='Process bulk records efficiently')     public static void processBulkRecords(List<MyObject__c> records) {         // Your bulkified logic here         List<SomeOtherObject__c> objectsToUpdate = new List<SomeOtherObject__c>();                  // Iterate over the input records         for (MyObject__c record : records) {             // Perform your business logic on each record             SomeOtherObject__c relatedObject = new SomeOtherObject__c();             rela...

Salesforce Process Builder with Invocable Methods

Image
Salesforce Process Builder is a powerful tool that allows you to automate business processes by creating workflows with point-and-click configuration. One of the features of Process Builder is the ability to call Invocable Methods, which are methods defined in Apex classes and annotated with @InvocableMethod. These methods can be invoked by Process Builder to perform more complex logic or interact with external systems. Here are some different use cases where you can leverage Process Builder in conjunction with User-related actions: User Onboarding Process : Automate the onboarding process for new users. When a new user record is created, trigger a process that assigns a specific profile, configures default settings, sends welcome emails, and assigns tasks for training. User Permissions Management : Streamline user permissions management by automatically adjusting user roles, profiles, or permission sets based on certain criteria, such as job role changes or team assignments. User Not...

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