Tag Archives: Apex

Difference between Standard Controller and Custom Controller?

Standard Controller:

  • Standard controller in apex, inherits all the standard object properties and standard button functionality directly.
  • It contain the same functionality and logic that are used for standard Salesforce pages.
  • It Can be used with standard objects and custom objects.
  • It can be extended to implement custom functionality using extensions keyword.
  • It provides a save method to allow you to persist changes.
  • You’d use this when you have a singe object to manipulate.

Custom Controller:

  • It is an Apex class that implements all of the logic for a page without leveraging a standard controller.
  • Custom Controllers are associated with Visualforce pages through the controller attribute.
  • Custom controller defines its own functionality.
  • Use custom controllers when you want your Visualforce page to run entirely in system mode, which does not enforce the permissions and field-level security of the current user.
  • You’d use this when your page isn’t dealing with a main object.

Send Email Using Apex In Salesforce

Public void SendEmailMethod(){
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    String[] toAddresses = new String[] {'test1@mail.com','test2@mail.com'}; 
    String[] ccAddresses = new String[] {'testcc1@mail.com','testcc2@mail.com'};
    mail.setToAddresses(toAddresses);
    mail.setCcAddresses(ccAddresses);
    mail.setReplyTo('youremail@mail.com');
    mail.setSenderDisplayName('Your Name');
    mail.setSubject('Testing email through apex');
    mail.setBccSender(false);
    mail.setUseSignature(false);
    mail.setPlainTextBody('This is test email body.');
    //mail.setHtmlBody('<b> This is HTML body </b>' );//Enable if you want HTML content
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}

Bypass Salesforce Duplicate Rule in Apex Class

Sample Code:

Database.DMLOptions dbDML = new Database.DMLOptions(); 
dbDML.DuplicateRuleHeader.allowSave = true;
dbDML.DuplicateRuleHeader.runAsCurrentUser = true;
Account duplicateAcc = new Account(Name='Salesforce');
Database.SaveResult sr = Database.insert(duplicateAcc, dbDML);
if (sr.isSuccess()) {
    System.debug('Duplicate account has been inserted in Salesforce!');
}

Data Import From CSV Using Visualforce Page

Sometimes we need to read a CSV file in a Visualforce Page. Here is an example to read a CSV file and display it in Visualforce Page. Below example reads a CSV file having Account records in it and displays them in a table.

CSV file format used in this example:

Controller:

Public with sharing class Sample {
    
    public Blob csvFileBody {get;set;}
    Public string csvAsString {get;set;}
    Public String[] csvFileLines {get;set;}
    Public String[] inputValues {get;set;}
    Public List<string> fieldList {get;set;}
    Public List<account> sObjectList {get;set;}
    
    public Sample(){
        csvFileLines = new String[]{};
            fieldList = New List<string>();
        sObjectList = New List<sObject>(); 
    }
    
    Public void readcsvFile(){
        csvAsString = csvFileBody.toString();
        csvFileLines = csvAsString.split('\n');
        inputValues = new String[]{};
            
            for(string st:csvfilelines[0].split(',')){
                fieldList.add(st); 
            }
        
        for(Integer i=1; i<csvfilelines.size(); i++){
            Account accRec = new Account() ;
            string[] csvRecordData = csvFileLines[i].split(',');
            accRec.Name = csvRecordData[0] ;             
            accRec.AccountNumber = csvRecordData[1];
            accRec.Type = csvRecordData[2];
            accRec.Industry = csvRecordData[3];                                                                               
            sObjectList.add(accRec);   
        }
    }
}

Visualforce Page:

<apex:page controller="Sample">
    <apex:form>
        <apex:pageBlock>
            <apex:panelGrid columns="2"> 
                <apex:inputFile value="{!csvFileBody}"  filename="{!csvAsString}"/>
                <apex:commandButton value="Read CSV" action="{!readcsvFile}"/>
            </apex:panelGrid>
        </apex:pageBlock>
        <apex:pageBlock>
            <apex:pageblocktable value="{!sObjectList}" var="rec">
                <apex:column value="{!rec.Name}" />
                <apex:column value="{!rec.AccountNumber}"/>
                <apex:column value="{!rec.Type}" />
                <apex:column value="{!rec.Industry}" />
            </apex:pageblocktable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Output:

Conditionally Render Textbox on Selection of a Radio Button in Visualforce Page

Controller:

public class Sample{
    
    public String selectedOption { get; set; }
    
    public Sample()
    {
        selectedOption = 'No';
    }
}

Visualforce Page:

<apex:page controller="Sample">
    <apex:form id="form">
        <apex:selectRadio value="{!selectedOption}">
            <apex:actionSupport event="onchange" rerender="form" />
            <apex:selectOption itemValue="Yes" itemLabel="Yes" />
            <apex:selectOption itemValue="No" itemLabel="No" />
        </apex:selectRadio>
        <apex:inputText rendered="{!selectedOption='Yes'}" />
    </apex:form>
</apex:page>

Output: