Trapdoor In The Sun

Alan Shanahan, Technician & Consultant

Force.com: Visualforce; slightly better field declarations

Leave a comment

Soft coded = good. Hard coded = bad. That’s difficult to argue against and I doubt there’s much dissent in any programming community.

Define it once, use it many times; this is part of the reusability principle that applies to code segments and text literals. When you define an input field in a Visualforce page, more often than not it is based on a field that exists somewhere in the database. It makes a whole lot of sense to take as many attributes from the original field definition as possible, so below are some examples of how you might do that.

(1) When defining a field on a page, prefix it with its label. Use this syntax:
$ObjectType.ObjectName__c.Fields.FieldName__c.Label
…where ObjectName__c and FieldName__c should be replaced as appropriate.

(2) Use the field’s own help text by referring to the InlineHelpText attribute, as shown below in this syntax:
$ObjectType.ObjectName__c.Fields.FieldName__c.InlineHelpText
…where ObjectName__c and FieldName__c should be replaced as appropriate.

(3) Limit the field length in HTML during data entry by using this syntax:
$ObjectType.ObjectName__c.Fields.FieldName__c.Length
…where ObjectName__c and FieldName__c should be replaced as appropriate.

<apex:pageBlock id="searchPageBlock">

	<apex:pageBlockSection columns="2"
	 id="searchPageBlockSection" title="Global Search" collapsible="false">

		<apex:pageBlockSectionItem id="searchAirport"
		 helpText="{!$ObjectType.Airport__c.Fields.Airport_Name__c.InlineHelpText}">

			<apex:outputLabel
			 value="{!$ObjectType.Airport__c.Fields.Airport_Name__c.Label}" />
			<apex:inputText
			 value="{!wrkAirportName}"
			 tabIndex="2"
			 id="inpAirportName"
			 maxlength="{!$ObjectType.Airport__c.Fields.Airport_Name__c.Length}"
			 onkeypress="return noenter(event);" />

		</apex:pageBlockSectionItem>
	</apex:pageBlockSection>
</apex:pageBlock>

The above code works well for text input fields. If you need information on numeric or other input field types check out the $ObjectType schema information page.

Advertisements

Author: Alan Shanahan

Cloud computing professional, amateur drummer and other things. Doesn't take himself too seriously. He's got a fever ... and the only prescription is more cowbell.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s