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
eriktowneriktown 

Form button not executing its action

Hello all! 

 

I have a visualforce page that has a user pick something from a selectList, then calls an Apex method called doSelect when the user hits the button on a form. However, for some reason the method isn't getting called when the user presses the button. I'm guessing I've made a mistake in the visualforce code somewhere, but I can't spot it. I'm a little suprised as I've done this many times before.

 

Here's the code:

 

<apex:page controller="PickerCon" action="{!autoRun}">
<apex:sectionHeader title="Purchase"/>
<apex:outputPanel id="msg">
    This item will be sent to: &nbsp;<apex:outputText value="{!email}"/> 


<apex:form >
<apex:pageBlock mode="edit">
<apex:pageblockSection title="Select an Item">
<apex:selectList value="{!selecteditem}">
            <apex:selectOptions value="{!items}"/>
        </apex:selectList>
</apex:pageblockSection>
<apex:pageBlockButtons >
          <apex:commandButton value="Purchase" action="{!doSelect}" rerender="msg"/>
          <apex:commandButton value="Cancel"/>
</apex:pageBlockButtons>

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

 

Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower

Perhaps try:

 

 

public class TCCardPickerCon {

    TCCardPickerCon() { // Not needed in this case, but I always put the constructor there.
    }
 
    public String selectedItem {get; set;}
    
    public List<SelectOption> getItems() {
        List<SelectOption> items = new List<SelectOption>();
        items.add(new SelectOption('101', 'test1'));
        items.add(new SelectOption('102', 'test2'));
        items.add(new SelectOption('104', 'test3'));
        return items;
    }
    
    public PageReference doSelect() {
         System.debug('doSelect entered');
// To be really good citizens, I suppose we should also use:
PageReference pageRef = Page.TCPurchase;
// and then add the parameters via the getParameters.put('cemail',... sort of thing.
This way Salesforce "knows" that your VF page is dependent on the other VF page and will enforce the dependencies. 
         PageReference pageRef = new PageReference(
                 'apex/TCPurchase' + 
                 '?cemail=' + ApexPages.currentPage().getParameters().get('cemail') + 
                 '&itemid=' + selectedItem
         );   
         pageRef.setRedirect(true);
         return pageRef;
    }
}
<apex:page controller="TCCardPickerCon ">
<apex:sectionHeader title="Purchase"/>

<apex:form>
<apex:pageBlock mode="edit">
<apex:pageBlockButtons >
          <apex:commandButton value="Purchase" action="{!doSelect}" rerender="msg"/>
<apex:commandButton value="Cancel" action="{!cancel}"/> </apex:pageBlockButtons>
// Adding this section
<apex:outputPanel>
The email parameter is: {!CurrentPage.parameters.cemail}
</apex:outputPanel> 
<apex:pageblockSection title="Select an Item">
<apex:selectList value="{!selecteditem}">
      <apex:selectOptions value="{!items}"/>
</apex:selectList>
</apex:pageblockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

 I noted that the Controller you named in the page was PickerCon, and your actual controller class was: TCCardPickerCon 

So, I'm surprised you were even able to save your class without errors.   the obvious thought is you have another class called PickerCon out there, and it doesn't have a debug statement in it?  :-)

 

Best, Steve

 

 

 

 

 

 

 

 

 

 

All Answers

Chamil MadusankaChamil Madusanka

Hi,

 

Do you suppose to pass the selected value from the pick list to the controller method?

If it is Yes, You need to use the <apex:param> and pass the parameter.

 

What is the functionality of doSelect method?

 

If you can post your controller. Then it will be easy to understand the issue.

 

If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

SteveBowerSteveBower

It looks pretty basic, and pretty correct to me at first read through.

 

I'd add layout="block" to the outputPanel (just to be cleaner), and you have no Cancel action defined.  You might also add a <apex:pageMessages /> to the page.

 

And, if you're reRendering msg, do you really want msg to cover the entire page, or just that top outputpanel?

 

But putting those aside, I can't see any reason doSelect wouldn't even be invoked.  I presume you're reading that from the debug log?   Clearly we have no idea what it's supposed to be doing, but not even called here seems wierd.

 

Best, Steve.

eriktowneriktown

So something like this?

 

          <apex:commandButton value="Purchase" action="{!doSelect}" rerender="msg">
              <apex:param name="selected" value="{!selectedItem}" assignTo="{!doSelect}"/>
          </apex:commandButton> 

 

 

I tried that, but it did not fix the problem. The problem is that doSelected is not being executed at all - I can see from the debug output that the method is never being called.

 

Here is the controller code:

 

public class TCCardPickerCon {

    
    public String email {get; set;}
    public String selectedItem {get; set;}
    
    public PageReference autoRun() {
        email = ApexPages.currentPage().getParameters().get('cemail');
        return ApexPages.currentPage();
    }
    
    public List<SelectOption> getItems() {
        List<SelectOption> items = new List<SelectOption>();
        items.add(new SelectOption('101', 'test1'));
        items.add(new SelectOption('102', 'test2'));
        items.add(new SelectOption('104', 'test3'));
        return items;
    }
    
    public void setEmail(String email) { this.email = email; }
    
    public PageReference doSelect() {
         System.debug('doSelect entered');
         PageReference pageRef = new PageReference('apex/TCPurchase?cemail=' + email + '&itemid=' + selectedItem);   
         pageRef.setRedirect(true);;
         return pageRef;
    }
}

 As you can see I have a debug message at the entrance to the doSelect method. Since that debug message doesn't appear in the logs I am forced to conclude that the method simply isn't being run for some reason.

 

Thanks for your help!

eriktowneriktown

Steve  - I don't want to rerender anything at all; actually what I want to do is go to another page, passing some information from this one in the query string. However, I remember reading somewhere that params don't get sent if you don't have a rerender tag in there. That's the only reason it's there.

SteveBowerSteveBower

Perhaps try:

 

 

public class TCCardPickerCon {

    TCCardPickerCon() { // Not needed in this case, but I always put the constructor there.
    }
 
    public String selectedItem {get; set;}
    
    public List<SelectOption> getItems() {
        List<SelectOption> items = new List<SelectOption>();
        items.add(new SelectOption('101', 'test1'));
        items.add(new SelectOption('102', 'test2'));
        items.add(new SelectOption('104', 'test3'));
        return items;
    }
    
    public PageReference doSelect() {
         System.debug('doSelect entered');
// To be really good citizens, I suppose we should also use:
PageReference pageRef = Page.TCPurchase;
// and then add the parameters via the getParameters.put('cemail',... sort of thing.
This way Salesforce "knows" that your VF page is dependent on the other VF page and will enforce the dependencies. 
         PageReference pageRef = new PageReference(
                 'apex/TCPurchase' + 
                 '?cemail=' + ApexPages.currentPage().getParameters().get('cemail') + 
                 '&itemid=' + selectedItem
         );   
         pageRef.setRedirect(true);
         return pageRef;
    }
}
<apex:page controller="TCCardPickerCon ">
<apex:sectionHeader title="Purchase"/>

<apex:form>
<apex:pageBlock mode="edit">
<apex:pageBlockButtons >
          <apex:commandButton value="Purchase" action="{!doSelect}" rerender="msg"/>
<apex:commandButton value="Cancel" action="{!cancel}"/> </apex:pageBlockButtons>
// Adding this section
<apex:outputPanel>
The email parameter is: {!CurrentPage.parameters.cemail}
</apex:outputPanel> 
<apex:pageblockSection title="Select an Item">
<apex:selectList value="{!selecteditem}">
      <apex:selectOptions value="{!items}"/>
</apex:selectList>
</apex:pageblockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

 I noted that the Controller you named in the page was PickerCon, and your actual controller class was: TCCardPickerCon 

So, I'm surprised you were even able to save your class without errors.   the obvious thought is you have another class called PickerCon out there, and it doesn't have a debug statement in it?  :-)

 

Best, Steve

 

 

 

 

 

 

 

 

 

 

This was selected as the best answer
eriktowneriktown

Actually I was trying to be stealthy and give my classes generic names, and I forgot to rename that one. The class name is consistent in the original code. :)

 

Steve, your code works - thanks! However, I'm wondering if there is still a way that I can print the email address when the page is loaded, as in my original version?

SteveBowerSteveBower

stealthy... :-)    I'm still not sure what was wrong with your original code...   I edited my solution to output the cemail.  You don't even need to involve the controller in this.

 

Also, it's a good idea (often anyway), to use a standard controller and write your stuff as a controller extension.  That way you get freebies like {!cancel} for free.   Sure, sometimes you just want a totally standardalone controller... but sometimes it's nice to have the help.

 

Best, Steve.

eriktowneriktown

Oh, perfect. This has been a very educational exchange for me; I'm just taking my first steps in Salesforce development.

 

Steve, thank you very much for your help, your excellent and thorough responses not only solved my problem but give me a better understanding of how this is all supposed to work. I've been really impressed with the community support here on the Developerforce forums, and you exemplify it.

SteveBowerSteveBower

Happy to help, especially with a lucid question from someone who obviously tried to solve it themselves before asking for advice.   Best, Steve.