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
Chanagan SakulteeraChanagan Sakulteera 

I have problem apex:commandButton

Hi all, Why <apex:commandButton> in visualforce page not calling method in controller.

This is code in visualforce page.
<apex:pageBlockTable value="{!addCoach}" var="addC" id="table" style="width:500px">
                <apex:column headerValue="Other Name">
                    <apex:inputField value="{!addC.ServiceName__c}" html-placeholder="Other Name"/>
                </apex:column>
                
                <apex:column headerValue="Currency" style="width:50px">
                        <apex:outputPanel layout="block" styleClass="requiredInput">
                            <apex:outputpanel layout="block" styleClass="requiredBlock"/> 
                            <apex:selectList value="{!addC.CurrencyIsoCode}" size="1" multiselect="false" required="true">
                                <apex:selectOption itemValue="" itemlabel="==Not Specify=="></apex:selectOption>
                                <apex:selectOptions value="{!MyCurrencyIso}"/>
                            </apex:selectList>
                        </apex:outputPanel>
                </apex:column>
                
                <apex:column headerValue="Total Cost">
                    <apex:inputText value="{!addC.Total_Cost__c}" html-placeholder="Total Coast"/>
                </apex:column>
                
                <apex:column >
                    <apex:commandButton value="Add Row" action="{!addRow}"/>
                </apex:column>

                
            </apex:pageBlockTable>

And this code in controller.
public with sharing class CostCalculationExtention{

public List<Product_Service__c> addCoach {get; set;}
//-- Constructor --\\
    public CostCalculationExtention(ApexPages.StandardController controller) {
        //-- Add Coach --\\
        addCoach = [select ServiceName__c, Total_Cost__c, CurrencyIsoCode from Product_Service__c where Id =: productId];
        addCoach.add(new Product_Service__c(Product__c = productId, Service__c = service.Id, ServiceName__c = '', CurrencyIsoCode = '',        
        Total_Cost__c = 0));
        //-- Add Coach --\\
    }
//-- Add Coach --\\
    public void addRow(){
        system.debug('Start Add Coach');
        addCoach.add(new Product_Service__c());
        system.debug('End Add Coach');
    }
//-- Add Coach --\\

}

Thank you in advance
NekosanNekosan
Did you try adding rerender attribute to commandbutton?
RishavRishav
Have you used the <apex:form> component as container. <apex:commandButton> must be a child of <apex:form> component.
Chanagan SakulteeraChanagan Sakulteera
Rishav i already use apex:form as a container and apex:commandButton is a child component.
This Code
<apex:page standardController="Product_Service__c" extensions="CostCalculationExtention" sidebar="false">
<apex:form>
<apex:pageBlock title="Coach">
            
            <br/>
            <apex:pageBlockTable value="{!addCoach}" var="addC" id="table" style="width:500px">
                <apex:column headerValue="Other Name">
                    <apex:inputField value="{!addC.ServiceName__c}" html-placeholder="Other Name"/>
                </apex:column>
                
                <apex:column headerValue="Currency" style="width:50px">
                        <apex:outputPanel layout="block" styleClass="requiredInput">
                            <apex:outputpanel layout="block" styleClass="requiredBlock"/> 
                            <apex:selectList value="{!addC.CurrencyIsoCode}" size="1" multiselect="false" required="true">
                                <apex:selectOption itemValue="" itemlabel="==Not Specify=="></apex:selectOption>
                                <apex:selectOptions value="{!MyCurrencyIso}"/>
                            </apex:selectList>
                        </apex:outputPanel>
                </apex:column>
                
                <apex:column headerValue="Total Cost">
                    <apex:inputText value="{!addC.Total_Cost__c}" html-placeholder="Total Coast"/>
                </apex:column>
                
                <apex:column >
                    <apex:commandButton value="Add Row" action="{!addRow}" rerender="table"/>
                </apex:column>

                
            </apex:pageBlockTable>
        </apex:pageBlock>
</apex:form>
</apex:page>

Nekosan i try your solution it is not working :(
NekosanNekosan
ok. Now you need to debug this code. First only keep commandbutton and remove everything. check debug logs. 

<apex:page standardController="Product_Service__c" extensions="CostCalculationExtention" sidebar="false">
<apex:form>

<apex:commandButton value="Add Row" action="{!addRow}" rerender="table"/>
</apex:form>
</apex:page>

 
Chanagan SakulteeraChanagan Sakulteera
Nekosan ok i follow your code and this is what i got

debug log.
15:09:20.044 (44543699)|USER_DEBUG|[84]|DEBUG|Start Add Coach
15:09:20.044 (44558945)|SYSTEM_METHOD_EXIT|[84]|System.debug(ANY)
15:09:20.044 (44584126)|SYSTEM_METHOD_ENTRY|[85]|CostCalculationExtention.__sfdc_addCoach()
15:09:20.044 (44619380)|SYSTEM_METHOD_EXIT|[85]|CostCalculationExtention.__sfdc_addCoach()
15:09:20.044 (44825114)|SYSTEM_METHOD_ENTRY|[85]|LIST<Product_Service__c>.add(Object)
15:09:20.044 (44847601)|SYSTEM_METHOD_EXIT|[85]|LIST<Product_Service__c>.add(Object)
15:09:20.044 (44864526)|SYSTEM_METHOD_ENTRY|[86]|System.debug(ANY)
15:09:20.044 (44882191)|USER_DEBUG|[86]|DEBUG|End Add Coach
 
lakslaks
Hi,

From the Debug log it is clear that the controller method is invoked. 
Chances are that there is some other error occuring on the page, which is causing the change caused by the controller invocation to not reflect on the page.

Adding the <apex:pageMessages/> component to your page can help in identifying any other error that might be occuring.

Regards,
Lakshmi.