Posts

Salesforce Cron expression for Scheduling the Job

Image
In Salesforce, you can use cron expressions to schedule jobs to run at specific times or intervals. Cron expressions are strings comprised of six or seven fields that represent various components of time (seconds, minutes, hours, days, months, and optionally, years).  Apex Scheduler Note Salesforce schedules the class for execution at the specified time. Actual execution can be delayed based on service availability.The System.schedule method uses the user's timezone for the basis of all schedules. Here's the format: Seconds Minutes Hours Day_of_month Month Day_of_week Optional_year The following are the values for the expression: The special characters are defined as follows: The following are some examples of how to use the expression. Here are some examples of cron expressions commonly used for scheduling jobs in Salesforce: Every day at midnight: 0 0 0 * * ? Every hour: 0 0 * * * ? Every 15 minutes: 0 0/15 * * * ? Every weekday at 9 AM: 0 0 9 ? * MON,TUE,WED,THU,FRI Every 30...

Salesforce Batchable Apex and Schedulable Apex with an Example

Salesforce provides two powerful features for executing code asynchronously: Batchable Apex and Schedulable Apex. These features allow developers to perform complex data processing tasks, integrations, or other long-running operations without impacting the user interface or causing performance issues. Below, I'll explain each feature along with an example for better understanding. Batchable Apex: Batchable Apex allows you to process large data sets in smaller, more manageable batches. It is particularly useful when dealing with large volumes of records that cannot be processed synchronously due to Salesforce's governor limits. Here's an example of how Batchable Apex works: // Example Batchable Apex class global class MyBatchableClass implements Database.Batchable<sObject> {          global Database.QueryLocator start(Database.BatchableContext BC) {         // Query the records you want to process         return Datab...

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