Category Archives: Salesforce

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.

Salesforce Apex Trigger Events

  • Before Insert: Trigger that executes due to an insert operation and before the record is saved to the database it is a before Insert trigger
  • Before Update: Trigger that executes due to an update operation and before the record is saved to the database it is a before update trigger
  • Before Delete: Trigger that executes due to a delete operation and before the record is saved to the database it is a before delete trigger
  • After Insert: Trigger that executes due to an insert operation but after the record is saved to the database it is an after Insert trigger
  • After Update: Trigger that executes due to an update operation and after the record is saved to the database it is an after update trigger
  • After Delete: Trigger that executes due to a delete operation and after the record is deleted from the database it is an after delete trigger
  • After Undelete: Trigger that executes due to a undelete operation and after the record is undeleted from the database it is an after undelete trigger

Action Poller in Visualforce Page

Action poller acts as a timer in visualforce page. It sends an AJAX request to the server according to a time interval (time interval has to be specified or else it defaults to 60 seconds). Each request can result in a full or partial page update.
In this article I will demonstrate how to use actionpoller in visualforce page.

  • In the action attribute a controller method gets called. The method gets called with a frequency defined by the interval attribute which has to be greater than or equal to 5 seconds.
  • Time out can also be specified as an attribute in actionpoller. Once the time out point is reached it stops making AJAX callout to the server and controller method is no more called.

Create Apex class with following code:

Public with sharing class TestActionPoller
{
    Public  Integer Total{get;set;}
     
    Public TestActionPoller()
    {
        Total = 0;
    }
  
    Public void CountMethod()
    {
        Total++;
    }
}

Now create the Visualforce page:

<apex:page controller="TestActionPoller">
<apex:form>
<apex:outputtext id="idCount" value="Increase in every 5 seconds: {!Total}">
<apex:actionpoller action="{!CountMethod}" interval="5" rerender="idCount">
</apex:actionpoller>
</apex:outputtext></apex:form>
</apex:page>

In above visualforce page Action poller calls the method “CountMethod” every 5 seconds where the variable “Total” is updated. Rerender attribute refreshes the page block hence showing the updated value of variable “Total”.

download

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 });
}

Calling Visualforce Page From Javascript

Visualforce page that calls another Visualforce page upon confirming from javascript.

Visualforce Page:

<apex:page standardController="Account">
    <apex:form>
        <apex:pageBlock>
            <apex:commandButton value="Open VF Page" onclick="OpenVFPage()"/>
        </apex:pageBlock>
    
    <script>
      function OpenVFPage(){
        var isConfirm = confirm('Do you want to open a new Visualforce Page?');
        if(isConfirm)
           window.open('/apex/YourVisualForcePage');
      }
     </script>
    </apex:form>
</apex:page>

Similarly, if you want to open a visualforce page from a custom button using javascript then use following piece of code.

{!REQUIRESCRIPT("/soap/ajax/22.0/connection.js")} 
var isConfirm = confirm('Do you want to open new Visualforce Page?');
if(isConfirm){
	window.parent.location.href="/apex/YourVisualforcePage";
}