Posts

Showing posts from 2013

Pass by Value or Pass by Reference in Salesforce

Pass by Value: When you pass a primitive data type (such as Integer, Boolean, String) as an argument to a method, a copy of the value is passed to the method. Any changes made to the parameter within the method do not affect the original value of the variable passed in. This behavior is consistent with most programming languages and is commonly referred to as pass-by value. Example: public void incrementValue(Integer x) {     x++; // Changes made to 'x' do not affect the original value of the variable passed in } Integer value = 10; incrementValue(value); System.debug(value); // Output: 10 (unchanged) Pass by Reference (for non-primitive data types): When you pass a non-primitive data type (such as List, Map, Set, or custom objects) as an argument to a method, a reference to the original variable is passed to the method. This means that changes made to the parameter within the method can affect the original value of the variable passed in. However, assigning a new value to the...

Importing a CSV file using Apex and Visualforce

Here is an example of importing a CSV file using Apex and Visualforce. Depending on your requirements, you may need to enhance this solution with additional error handling, validation, and business logic. Visualforce Page- <apex:page controller="demoImportDataFromCSV">     <apex:form >         <apex:pagemessages />         <apex:pageBlock >             <apex:pageBlockSection columns="2">                    <apex:inputFile value="{!csvFileBody}"  filename="{!csvAsString}"/>                   <apex:commandButton value="Import Product" action="{!importCSVFile}"/>             </apex:pageBlockSection>         </apex:pageBlock>         <apex:pageBlock >           ...

Visualforce View State

Image
In Visualforce, view state refers to the state of the page, including the state of its form data, component values, and controller state, that is transmitted to and from the server with each request. View state enables Visualforce pages to maintain stateful behavior similar to traditional web applications, where user inputs and interactions are preserved across multiple requests. Optimize the View State View State Tab Steps to reduce the View State Use filters and pagination to reduce data that requires state. Declare an instance variable with a transient keyword if the variable is only useful for the current request. A transient variable isn’t included in the view state. Refine your SOQL calls to return only data that's relevant to the Visualforce page. Reduce the number of components that your page depends on. Make data read-only. Use the <apex:outputText> component instead of the <apex:inputField> component. Use JavaScript remoting. Unlike the <apex:actionFunction...

Retrieve current record id in salesforce

To get the current record ID in Salesforce Visualforce, you can use the Id parameter in the URL or leverage standard controllers. Here are two common methods to accomplish this: Using the Id Parameter in the URL: In Visualforce, you can access the current record's ID using the Id parameter in the URL. This method is commonly used when creating custom pages or links. <apex:page standardController="Account">     <h1> Account Record ID: {!$CurrentPage.parameters.Id}</h1> </apex:page> In this example, {!$CurrentPage.parameters.Id} retrieves the ID from the URL. Using Standard Controllers : If you're using a standard controller in your Visualforce page, you can directly reference the record ID using the Id property of the standard controller. <apex:page standardController="Account">     <h1>Record ID: {! Account.Id}</h1> </apex:page> In this example, {! Account.Id} retrieves the ID of the current record from the st...

How to Avoid recursive trigger in salesforce ?

Avoiding recursive triggers in Salesforce is crucial to prevent infinite loops and excessive consumption of system resources. Here are several strategies to achieve this: Use a Static Variable or Flag : Utilize a static Boolean variable or a static set to keep track of whether the trigger logic has already been executed. This prevents the trigger from re-executing when it's not necessary. public class TriggerHandler {     private static Boolean isFirstRun = true;     public static void onBeforeInsert(List<MyObject__c> newList) {         if (isFirstRun) {             // Trigger logic             isFirstRun = false;         }     } } Use a Handler Class : Implement a separate handler class to encapsulate trigger logic. This class can have static variables to manage recursion and can be called from the trigger. public class TriggerHandler {     p...

Use of render, rerender, and renderAs in Salesforce

In Salesforce, render, rerender, and renderAs are terms used in the context of Visualforce pages and components. Each term relates to a different aspect of how Visualforce content is displayed or processed. render determines whether a component should be displayed on the page based on a condition. rerender specifies which components should be updated with new data after an AJAX action. renderAs defines the format in which the Visualforce page should be rendered, such as PDF, Excel, or CSV. render : Usage : Used in Visualforce components and tags to define the conditions under which the component or tag should be rendered on the page. Description : The render attribute specifies a Boolean value or expression that determines whether the associated component or tag should be rendered (displayed) on the page. If the value evaluates to true, the component is rendered; otherwise, it is omitted from the page's HTML output. Example : <apex:outputText value="Hello World!" re...

Using WITH SHARING and WITHOUT SHARING keywords

In Salesforce Apex, the with sharing and without sharing keywords are used to control the sharing behavior of data queried or manipulated within a class. These keywords enforce the sharing rules defined in Salesforce for the current user executing the Apex code. Here's how these keywords work: with sharing : When a class is declared with the with sharing keyword, the sharing rules defined in Salesforce are enforced. This means that the class respects the organization-wide defaults (OWD), role hierarchies, sharing rules, and manual sharing settings. Users only have access to records they are permitted to see according to these rules. public with sharing class MyClass {     // Apex code here } without sharing : When a class is declared with the without sharing keyword, the sharing rules defined in Salesforce are bypassed. This means that Apex code within the class runs in system context and ignores any sharing rules, allowing it to access all records regardless of the user's sha...

Use the Developer Console to force password resets in Salesforce

Image
When using the Developer Console to force password resets for newly created users, the setPassword method of System Class may redirect users to the "Change Your Password" page. Clicking on any text box allows the user to log in to the org without setting the security questions.   Note: If you have used setPassword() to configure a user's password and they have never set a security question, they will be redirected to the Change Your Password page upon login. Steps to set a password  Open Developer Console Open the Execute anonymous window > Write : system.setPassword('005xx0000000XXX', 'passwordText');  i.e System.setPassword(userid, password); Click Execute Click on the gear icon below the screenshot and select ‘ Developer Console’ A new window will open - You need to click on Debug → Open execute Anonymous Window A new window will open, add the below command, and click on execute.  System.setPassword(userid, password);