Tag Archives: Visualforce Page

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

Query Rows & Collection size exceeds maximum limit

Visualforce Page:

<apex:page controller="yourCustomcontroller" readonly="true">  
</apex:page>  

Please note using readonly="true" in Visualforce page, can increase Query Rows & Collection size:

  • Query Rows limits increased from 50001 to 1 million rows.
  • Collection size limits increased from 1001 to 10000.

Pass variable to a custom label from visualforce page

To know more about custom label Click here
Step 1:
Go to Setup –> App Setup –> Custom Labels.
Step 2:

1

Step 3:
Here for dynamic populate custom label variable value, the custom label value has written like this “My name is {0} {1}”.

2

Step 4:
Now I’ll create an apex controller with two variable to send it custom label.

public class TestCustomLabel{
    public string FirstName{get;set;}
    public string LastName{get;set;}    
     
    public TestCustomLabel(){
        FirstName = 'Biswajeet';
        LastName = 'Samal';
    }
}

Step 5:
Now I’ll create a visualforce page and use the custom label with parameters in it. Here the first parameter “FirstName” is for custom label “{0}” and the second parameter “LastName” is for custom label “{1}”.

<apex:page controller="TestCustomLabel" tabstyle="Account">
    <apex:form>
        <apex:pageblock>
            <apex:outputtext value="{!$Label.TestCustomLabel}">
            <apex:param value="{!FirstName}">
            <apex:param value="{!LastName}">
            </apex:param></apex:param></apex:outputtext>
        </apex:pageblock>
    </apex:form>
</apex:page>

Here is the visualforce page output:

3