Contents[Hide]

Debugging

Always use <apex:pageMessages id="debug" showDetail="TRUE" /> tag during developement it can display system errors during execution which are not shows during compile time (when saving) and are hard to filter from debug log.

Additionally you can pas there your own errors by:

ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, 'errorMsg', 'details'));
ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, 'errorMsg'));

Where details are optional.

Add error message to field.

objectName.fieldName__c.addError('This value is notvalid.');

Addotional info: Field Level Error Messages with Visualforce - Part 1 / Part 2

Picklist - one selection

Page component

<apex:selectList label="System" value="{!systemSelecedOption}" id="systemSelecedOption" size="1" multiselect="FALSE">
   <apex:selectOptions value="{!systemOptions}"/>
</apex:selectList> 

Controller - reading selected value

public String systemSelecedOption {get; set;}

Controller - fill the options

    public List<SelectOption> getSystemOptions() {
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('ALL','All'));
        options.add(new SelectOption('Calimaster','Calimaster'));
        options.add(new SelectOption('Vibguard Viewer','Vibguard Viewer'));       
        return options;
    }

Input text filed - standard get and set methods

Component description.

Visualforce code

<apex:inputText id="activationCode" label="Activation Code" value="{!activationCode}"/>

You can use default get set methods:

public String activationCode { get; set; }

And then use this fied as normal variable to read/write data like:

activationCode = 'Text displayed in field on page load'; // Passing value to page (getter - page get it)
enteredCode = activationCode // Reading value from page (setter - page firstly set it)

Input text filed - cutom get and set methods

Own custom getter and setter (you may read this SF article to get more understanding)

public String activationCodeValue = '';

    /****************** Getters ********************/
    /**** Passing values to page (page get it) *****/
    /***********************************************/
  
   public String getActivationCode() {      
        activationCodeValue = 'Text displayed in field on page load'; // Do something to prepare value 
        return activationCodValue; // Return value that can be read by VS Page
    }

    /***************** Setters *********************/
    /** Reading values from page (page sets value) */
    /***********************************************/
    public void setActivationCode(String s) {
        activationCodeValue = s; // Write value retirved from page to controller variable
    }

Disable apex:inputText with javascript

<script>document.getElementById('{!$Component.activationCode}').disabled = 1; </script>

http://www.tehnrd.com/how-to-disable-visualforce-inputfields/

AJAX reRender

When using reRender modifier with given ID of big part of page there is possibility to limit refresh action to specific region. It will reduce amount of data to be transferred and speed up refresh. You need to specify region by closing it beetween:

<apex:actionRegion>

</apex:actionRegion>

Filed with button as one line item

<apex:pageBlockSectionItem >
    <apex:outputLabel value="Account" for="accountField"/>
    <apex:outputPanel layout="block">             
        <table>
            <tr><td style="vertical-align: middle;"><apex:inputField id="accountField" value="{!Order_Processing__c.Account__c}"/></td>
            <td><apex:commandButton value="Read Adresses" action="{!doReadAccountAddresses}" style="margin-left:15px;"/></td></tr>
        </table>
    </apex:outputPanel>
</apex:pageBlockSectionItem>

Mark filed as required without setting it required.

There may be a situation that you need to mark field as mandatory using red bar but you can not set required="TRUE" as it breaks page logic.

Visualforce with standard required:

<apex:inputField id="Variant" value="{!ptt.ptt_Variant__c}" required="TRUE" />

Mark field with red bar but not reqire it directly

<!-- Variant field with reqired red mark -->
<apex:pageBlockSectionItem >  
    <apex:outputLabel value="Variant" for="Variant" />
    <apex:outputPanel styleClass="requiredInput" layout="block">
        <apex:outputPanel styleClass="requiredBlock" layout="block"/>
            <apex:inputField id="Variant" value="{!item.Variant__c}" />
    </apex:outputPanel>    
</apex:pageBlockSectionItem>

Visual force controller code which add error to field:

ptt.ptt_Variant__c.addError('Please select Variant');

Get field label from schema:

<apex:outputLabel value="{!$ObjectType.ObjectName.fields.FieldName.Label}" for="Variant" />

 

   
© Paweł Woźniak