function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
arosysarosys 

Custom Controller Extension

This is my visual force page :

 

<apex:page standardController="Transaction__c" extensions="myControllerExtension">

<apex:form >
<h1>New Transaction</h1>
<apex:pageBlock >

<apex:pageBlockButtons location="top">
<apex:outputLabel value="Select Transaction Type:"/>
<apex:selectList multiselect="false" size="1" value="{!selectedValue}">
<apex:selectOptions value="{!options}"/>
<apex:actionSupport event="onchange" reRender="pbSection" status="pStatus"/>
</apex:selectList>
<apex:actionStatus id="pStatus" startText="Rendering..."/>
</apex:pageBlockButtons><br/><br/>

<apex:pageBlockSection id="pbSection" >
<apex:pageBlockSectionItem rendered="{!selectedValue='Credit'}">
<apex:outputLabel value="Credit:"/>
<apex:inputField value="{! Transaction__c.Credit__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem rendered="{!selectedValue='Debit'}">
<apex:outputLabel value="Debit:"/>
<apex:inputField value="{! Transaction__c.debit__c}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<br/><br/>

<apex:pageBlockSection >
<apex:inputField value="{! Transaction__c.To_Account_Number__c}"/><br/>
</apex:pageBlockSection>
<apex:pageBlockSection >
<apex:inputField value="{! Transaction__c.Transaction_Amount__c}"/><br/>
</apex:pageBlockSection>
<apex:pageBlockSection >
<apex:inputField value="{! Transaction__c.Transaction_Date__c}"/><br/>
</apex:pageBlockSection>

<apex:pageBlockSection >
<apex:commandButton action="{! save}" value="Save!"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>

</apex:page>

 

 

And this the apex class :

 

public with sharing class myControllerExtension {

    private final Transaction__c trans;
    
    // The extension constructor initializes the private member
    // variable acct by using the getRecord method from the standard
    // controller.
    public myControllerExtension(ApexPages.StandardController stdController) {
        this.trans= (Transaction__c)stdController.getRecord();
    }

    public String selectedValue { get; set; }

    public List<SelectOption> getOptions() {
        List<SelectOption> options = new List<SelectOption>();
       
        options.add(new SelectOption('Credit','Credit'));
        options.add(new SelectOption('Debit','Debit'));
        return options;
    }
    
    public myControllerExtension () {
        selectedValue = 'Credit';
    }


   
}

 

 

My problem is that when i am running this page , I cannot see "Credit" & "Debit" input field.

 

In my apex class i had defined constructor , initializing its value to "Credit" but input field is not seen not even after changing it to debit.

 

Please help.

Best Answer chosen by Admin (Salesforce Developers) 
Puja_mfsiPuja_mfsi

Hi,

You need to initialize the "selectedValue" variable in the constructor which takes the Parameter of ApexPages.StandardController Type,because your page is use the standard controller.

so Add 

 

public myControllerExtension(ApexPages.StandardController stdController) {
        this.trans= (Transaction__c)stdController.getRecord();

        selectedValue = 'Credit';
}

 

If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.

All Answers

Karthikeyan JayabalKarthikeyan Jayabal
Initialize the selectedValue variable in the same constructor method: public myControllerExtension(ApexPages.StandardController stdController) itself & not in a separate constructor method.
Puja_mfsiPuja_mfsi

Hi,

You need to initialize the "selectedValue" variable in the constructor which takes the Parameter of ApexPages.StandardController Type,because your page is use the standard controller.

so Add 

 

public myControllerExtension(ApexPages.StandardController stdController) {
        this.trans= (Transaction__c)stdController.getRecord();

        selectedValue = 'Credit';
}

 

If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.

This was selected as the best answer