Tuesday, January 28, 2014

Export table data in Excel - Salesforce

Here is the easiest way to export table data which is displayed in VF page in excel file.

AccountData.page : VF page which is displaying Accounts data

<apex:page controller="AccountData">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!accList}" var="acc">
                <apex:column value="{!acc.Name}"/>
    <apex:column value="{!acc.Type}"/>
    <apex:column value="{!acc.Industry}"/> 
   </apex:pageBlockTable>
        </apex:pageBlock>
    <apex:commandButton value="Export" action="/apex/AccountDataExport"/ >
 </apex:form>
</apex:page>

AccountDataExport.page : VF page which is exporting Accounts data in Excel

Add ContentType attribute in apex page tag and set its value to application/vnd.ms-excel. Here in given example
AccountData.xls is the name of the file.

contentType="application/vnd.ms-excel#AccountData.xls"


<apex:page controller="AccountData" contentType="application/vnd.ms-excel#AccountData.xls">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!accList}" var="acc">
                <apex:column value="{!acc.Name}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

AccountData.cls :

public class AccountData{
    public List<Account> accList{get;set;}
    public AccountData(){
        accList = [Select Name,Type,Industry from Account limit 1000];
    }
}

Different ways to run Schedule class

To invoke Apex classes to run at specific times we are creating schedule class. 3 different ways are there to run Schedule class. Depends on the requirement we can decide which is the best way to call schedule class.

  1. Schedule Apex - UI based Standard way of salesforce
  2. Run Schedule class from developer console
  3. Call Schedule class from Trigger
Schedule class

global with sharing class ScheduleClass implements Schedulable {
    global void execute(SchedulableContext sc) {
        //Code which will run when schedule class runs 
    }
}

Schedule Apex - UI based Standard way of salesforce

Using salesforce standard feature, we can run Schedule class at specific time. Steps to schedule class.
  • Go to Setup > Build > Develop > Apex Classes
  • Click on 'Schedule Apex' button(refer below snap)





Run Schedule class from developer console

Many times situation occurs, when we wants to run schedule class immediately. Testing is one of them example where we want to run Schedule class immediately.

To run schedule class from Developer Console below follow steps.

  • Go to Developer Console > Open Execute Anonymous Window
  • Add bellow code and run

ScheduleClass temp = new ScheduleClass(); // ScheduleClass - Class which you want to Schedule
String sch = '0 10 23 * * ?'; // "0 10 23 * * ?" - "Seconds Minutes Hours Day_of_month Month Day_of_week optional_year" in order
system.schedule('ScheduleClass', sch, temp);
For more details visit salesforce site :  
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_System_System_schedule.htm



Call Schedule class from Trigger

We can call Schedule class the same way we are calling from Developer Console directly in trigger also. Refer bellow code.

trigger AccountOwnerChange on Account (before insert) {
    if(trigger.isInsert){
        ScheduleClass temp = new ScheduleClass();
        String sch = '0 10 23 * * ?'; //
        system.schedule('ScheduleClass', sch, temp);
    }
}