Tag Archives: Visualforce Page

How to Print a Visualforce page in Salesforce?

Controller:

public class PrintSample {
 
    public List<account> objList {get; set;}
    
    public PrintSample() {
        objList = new List<account>();
        objList = [SELECT Name, Phone FROM Account];
    }
}

Visualforce Page:

<apex:page showheader="false" controller="PrintSample">
    <apex:panelgrid width="100%" style="text-align:right;">
        <apex:form>
            <apex:commandlink value="Print" onclick="window.print();"></apex:commandlink>
        </apex:form>
    </apex:panelgrid>
    <apex:pageblock title="Account Information">
        <apex:pageblocktable value="{!objList}" var="a">
            <apex:column value="{!a.Name}"></apex:column>
            <apex:column value="{!a.Phone}"></apex:column>
        </apex:pageblocktable>
    </apex:pageblock>
</apex:page>

Output:

download

Rendering a Visualforce Page in PDF Format

You can render any page as a PDF by adding the renderAs attribute to the component, and specifying pdf as the rendering service.

For example:

<apex:page renderas="pdf">
</apex:page>

Note: Visualforce pages rendered as PDFs will either display in the browser or download as a PDF file, depending on your browser settings.

Confirm Dialog box in Visualforce page

Visualforce Page:

<apex:page>
    <apex:form>
        <apex:pageblock>
            <apex:pageblocksection title="Confirm dialog box Demo" collapsible="false">
                 <apex:commandbutton value="Click to Confirm" onclick="return confirm('Do you want to submit');"></apex:commandbutton>
            </apex:pageblocksection>
        </apex:pageblock>
    </apex:form>
</apex:page>

download

Calculate Age from Date of Birth using Apex in Salesforce

Apex Class:

public class CalculateAge
{  
    public Integer age {get; set;}    
    public Date dt {get; set;}
    
    public void FindAge()
    {
        Integer days = dt.daysBetween(Date.Today());
        age = Integer.valueOf(days/365);
    }
}

Visualforce Page:

<apex:page doctype="html-5.0" controller="CalculateAge">
    <apex:form>
        <apex:pageblock title="Calculate Age From Date of Birth">
            <apex:pageblocksection>
                <apex:pageblocksectionitem>Date of Birth:
                    <apex:inputtext onfocus="DatePicker.pickDate(true, this , false);" value="{!dt}"></apex:inputtext></apex:pageblocksectionitem>
                    <apex:commandbutton value="Get Age" action="{!FindAge}"></apex:commandbutton>
                    <apex:pageblocksectionitem>Age:           
                    <apex:outputtext value="{!age}"></apex:outputtext>
                </apex:pageblocksectionitem>           
            </apex:pageblocksection>
        </apex:pageblock>
    </apex:form>   
</apex:page>

Output:

download

How to use apex:detail in visualforce page?

Apex Controller:

public class TestApexDetail {
    public Id AccountId {get;set;}
  
    public TestApexDetail() {
        Account objAC = [SELECT Id From Account LIMIT 1];
        AccountId = objAC.Id;
    }
}

Visualforce Page:

<apex:page controller="TestApexDetail">
    <apex:form>
        <apex:detail subject="{!AccountId}" relatedlist="false"></apex:detail>
    </apex:form>
</apex:page>

Output:

download