Posts

Dynamic Apex in Salesforce

Dynamic Apex in Salesforce refers to the ability to construct and execute code dynamically at runtime, rather than having it defined statically at compile time. This feature allows developers to build more flexible and adaptable solutions that can respond to changing requirements or conditions during program execution. Dynamic Apex enables developers to create more flexible applications by providing them with the ability to: Access sObject and field describe information- Metadata information about sObject and field properties includes details about operations supported by sObject types (such as create or undelete), name and label of sObjects, fields and child objects. Field information contains details like default value presence, calculated field status, and field type Apex provides two data structures and a method for sObject and field describe information: Token —a lightweight, serializable reference to an sObject or a field that is validated at compile time. This is used for token...

Add a Lightning application within a Visualforce page in Salesforce

  <!-- create a lightning app with global access that extends lightning outApp-->   YourLightningComponent.app <aura:application access="GLOBAL" extends="ltng:outApp">        <aura: dependency resource="lightning: button"/>   </aura: application>   <apex:page >     <apex:includeLightning />          <div id="lightningContainer"></div>     <script>         $Lightning.use("c:YourApp", function() {             $Lightning.createComponent(                 "c:YourLightningComponent",                 {},                 "lightningContainer",                 function(cmp) {                   ...

Dynamic SOQL (Salesforce Object Query Language) and SOSL (Salesforce Object Search Language)

Dynamic SOQL (Salesforce Object Query Language) and SOSL (Salesforce Object Search Language) are powerful tools in the Salesforce ecosystem that allow developers to construct queries and search statements dynamically based on runtime conditions. This flexibility enables developers to build more dynamic and adaptable solutions. Here's an overview of dynamic SOQL and SOSL: Dynamic SOQL: Dynamic SOQL allows you to construct queries at runtime using string manipulation techniques. This is particularly useful when you don't know the structure of the query until the application is running. Here's a basic example of dynamic SOQL in Apex: String query = 'SELECT Id, Name FROM Account'; // You can add conditions dynamically based on runtime logic if(someCondition) {     query += ' WHERE CreatedDate > TODAY'; } List<Account> accounts = Database.query(query); Dynamic SOSL: Dynamic SOSL allows you to construct search queries dynamically. SOSL is used to search...

Salesforce MIXED_DML_OPERATION

The MIXED_DML_OPERATION error in Salesforce occurs when you attempt to perform a DML (Data Manipulation Language) operation on setup objects (such as User, Profile, UserRole, Group, etc.) within the same transaction as DML operations on regular sObjects (standard or custom objects).  This restriction is in place to maintain the integrity and security of Salesforce data because setup objects control system-wide settings and configurations that could impact multiple users and processes. Here's how you can handle the MIXED_DML_OPERATION error: Separate Setup and Non-Setup DML Operations : Split your DML operations into separate transactions, with one transaction handling DML operations on regular sObjects and another transaction handling DML operations on setup objects. public void handleMixedDML() {     // Perform DML operations on regular sObjects     List<Account> accountsToUpdate = [SELECT Id, Name FROM Account LIMIT 10];     // Perform DML oper...

Pagination in Visualforce using StandardSetController

Image
The StandardSetController is a controller class that enables you to create a Visualforce page to showcase a list of records. It has built-in capabilities to handle pagination, thus simplifying displaying extensive data sets on a Visualforce page. Here are some key features and functionalities of StandardSetController: Pagination : StandardSetController automatically handles pagination, allowing you to display a subset of records on each page. It provides methods for navigating between pages, such as first(), previous(), next(), and last(). Sorting and Filtering : StandardSetController allows you to specify sorting and filtering criteria for the records displayed on the Visualforce page. You can define sorting order and filter conditions using the setSortOrder() and setFilterId() methods. Record Navigation : You can easily navigate to the first, last, previous, or next page of records using methods provided by StandardSetController. This simplifies the implementation of navigation contr...

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