Posts

Validate Salesforce User

Image
 Here's a piece of code to verify the password of a user.            HttpRequest request = new HttpRequest();         request.setEndpoint('https://login.salesforce.com/services/Soap/u/22.0');         request.setMethod('POST');         request.setHeader('Content-Type', 'text/xml;charset=UTF-8');         request.setHeader('SOAPAction', '""');         request.setBody( prepareSoapLoginRequest ('me_raja1@test.com','QW@232323'));         ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, String.valueOf(new Http().send(request).getBody())));  public static String  prepareSoapLoginRequest (String username, String password){         XmlStreamWriter w = new XmlStreamWriter();         w.writeStartElement('', 'login', 'urn:partner.soap.sforce.com');         w.w...

Salesforce Workflow Rules vs Process Builder

Process Builder and Workflow Rules are automation tools in Salesforce that allow users to automate business processes without writing code. While they serve similar purposes, there are differences in their capabilities and use cases.  Workflow Rules: Purpose: Workflow Rules are used to automate standard business processes in Salesforce. They allow you to define criteria and actions to be executed when records meet specified conditions. Capabilities: Criteria: You can define entry criteria based on record field values or related objects. Immediate Actions: Actions such as field updates, email alerts, outbound messages, task creation, and posting to Chatter can be triggered immediately when the criteria are met. Time-Dependent Actions: You can schedule time-dependent actions, such as email alerts or task creation, to occur after a specified period. No Rollbacks: Workflow Rules do not support record rollbacks if an error occurs during execution. Usage: Workflow Rules are suitable for ...

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