Tag Archives: Salesforce.com

Display of Validation Rule Error on Visualforce Page

Here I’ve a custom “Student__c” object. There is a validation rule on “Date_of_Birth__c” field, that Date of Birth cannot be greater than today. And I’m using a visualforce page to insert the data in “Student__c” object. So, I need to show the validation rule error message in visualforce page.

Validation Rule:

Below is my controller which gave the solution for displaying validation error message on visualforce page.

Controller:

public with sharing class StudentExt {

    public Student__c student{get;set;}
    
    public StudentExt(ApexPages.StandardController controller) {
        student = (Student__c)controller.getRecord();
    }
    
    public Pagereference saveStudent() {
        try {
            Upsert student;
            return new Pagereference('/' + student.Id);
        }
        catch(DMLException de) {
            Apexpages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.FATAL, de.getDmlMessage(0)));
            return NULL;
        }
        catch(Exception e) {
            Apexpages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.FATAL, e.getMessage()));
            return NULL;
        }
    }
}

Visualforce Page

<apex:page standardController="Student__c" extensions="StudentExt" >
    <apex:pageMessages id="errormsg" />
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!saveStudent}" reRender="errormsg"/>
                <apex:commandButton value="Cancel" action="{!Cancel}"/>
            </apex:pageBlockButtons>
            
            <apex:pageBlockSection columns="2" title="Information">
                <apex:inputField value="{!Student__c.First_Name__c}"/>
                <apex:inputField value="{!Student__c.Last_Name__c}"/>
                <apex:inputField value="{!Student__c.Date_of_Birth__c}"/>
                <apex:inputField value="{!Student__c.Address__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Displaying Help Text in Visualforce Page

<apex:page standardController="Campaign" showHeader="true">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection title="Information">
                <apex:pageBlockSectionItem HelpText="{!$ObjectType.Campaign.fields.Category__c.inlineHelpText}">
                    <apex:inputField value="{!Campaign.Category__c}" label="{!$ObjectType.Campaign.fields.Category__c.label}" />
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Tab Title shown as External Page in Console

<apex:includeScript value="/support/console/20.0/integration.js"/>
<script type="text/javascript">
	function testSetTabTitle() {
		//Set the current tab's title
		sforce.console.setTabTitle('Account: {!Account.Name}');
	}
	var pageLoad = window.onload;
	window.onload = function() {
		testSetTabTitle();
	}
</script>

Disable filed in Visualforce page based on picklist values

Here in below example, I’ve a requirement on selection of “Category” picklist value “Product”, “Detail” inputfield will enable with required mark else will disable.

<apex:page standardController="Campaign" showHeader="true">    
    <apex:form >
        <apex:pageBlock>
            <apex:pageBlockSection title="Information" id="pb1">
                
                <apex:pageBlockSectionItem>
                    <apex:actionRegion>  
                        <apex:inputField label="{!$ObjectType.Campaign.fields.Category__c.Label}" value="{!Campaign.Category__c}">
                                <apex:actionSupport event="onchange" rerender="pb1" />
						</apex:inputField>
                    </apex:actionRegion>
                </apex:pageBlockSectionItem>
                
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="{!$ObjectType.Campaign.fields.Detail__c.label}" for="wurl" rendered="{!Campaign.Category__c=='Product'}"/> 
                    <apex:inputField id="wurl" value="{!Campaign.Detail__c}"  rendered="{!Campaign.Category__c=='Product'}"   required="{!Campaign.Category__c=='Product'}" />
                </apex:pageBlockSectionItem>
				
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Label does not get displayed when inputField is in an actionRegion

Action Region will wrap only the input Field and not the complete pageBlockSectionItem. Place pageBlockSectionItem tags inside a pageBlockSection, which in turn should be in a pageBlock. This will provide the correct formatting.

<apex:page standardController="Campaign" showHeader="true" >
	<apex:form>
		<apex:outputPanel id="op1">
			<apex:pageBlock>
				<apex:pageBlockSection columns="2" title="Information">
					<apex:pageBlockSectionItem>
						<apex:actionRegion>
							<apex:inputField label="{!$ObjectType.Campaign.fields.Category__c.Label}" value="{!Campaign.Category__c}"> 
								<apex:actionSupport event="onchange" rerender="op1" />
							</apex:inputField>
						</apex:actionRegion>
					</apex:pageBlockSectionItem>
				</apex:pageBlockSection>
			</apex:pageBlock> 
		</apex:outputPanel>
	</apex:form>
</apex:page>