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
Big EarsBig Ears 

Seeming inconsistency in <apex:commandbutton> behaviour

All,

 

I was wondering, are there any known cases / common mistakes that mean that commandbutton tags might stop calling their "action" method after the first few submits?

 

I have a page with some filters at the top, and a submit button that uses these filters to populate some tables below. These table contain record details and each row contains a commandlink to make more information from the selected row render beneath the tables.

 

This seems to work at first and then, at some point, the page seems to stop re-rendering or using the new information, meaning that the re-render appears to stop working.

 

Putting some debug code together, it seems that the action method isn't being called as well.

 

Because it's inconsistent, I can't pin down the exact issue. Also, the code is pretty complex, so I don't want to as people to go through it if there's an obvious set of mistakes that people make that I can check through.

 

With thanks for any and all help received,
Andy 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

This line:

 

list<selectOption> transactionHistoryProductOptions = new list<selectOption>();

 is declaring a local variable to the method within the scope of the if statement, which hides the controller property.  This is then discarded at the end of the if statement, and the null controller property is returned to the page.

 

If you change it to:

 

transactionHistoryProductOptions = new list<selectOption>();

 then you should be dealing with the property that the page is using.

All Answers

bob_buzzardbob_buzzard

When I've seen this kind of thing in the past, it was because there were errors with the post, but I wasn't rerendering an apex:pageMessages component along with the updated content, so this was hidden away from me.  

 

Another way this might happen is if there are onclick handlers for the commandbutton, but a rerender breaks the javascript.

AndyRouseAndyRouse

Bob,

 

Thanks for the feedback. I've had a pagemessages tag in the pagem but it's not being triggered, unfortunately. I've currently got the developer support team at Salesforce looking into it and will feedback what they say if/when they get to the bottom of it.

 

Andy

Big EarsBig Ears

Further information - It appears to be a problem with the multi-select list that forms part of the page.

 

As long as a single option is selected, it will work and the controller will receive the information. If multiple options are selected (after having been selecting single options and submitting and it all working), the controller stops receiving the submitted options. Any further submits are also ignored/not received by the controller.

 

Weirdly, this can be "fixed" by the user, if they select the first option in the list and submit, which seems to "clear" the problem and the page starts working again until the user tries to select multiple options and submit.

 

Weirdly, if the first submit is for multiple options, that will work and the controller will receive them, so it's not an issue with the controller not being able to handle multiple selected options ever. However, thereafter, the controller won't receive any values until the system is "reset" by selecting the first option and submitting.

 

Andy

bob_buzzardbob_buzzard

No downside on the user experience then ;)

Big EarsBig Ears

It's so odd!

 

Can you think of any reason why a mult-select list might cause an ajax post-back to fail? I say "ajax post-back", but I'm not sure of the exact mechanism here.

 

Thanks,

Andy

bob_buzzardbob_buzzard

Is this a standard field that renders the multi-select picklist, or a selectlist with the multiple attribute set to true?

AndyRouseAndyRouse

It's a selectList with the multi-select attribute. The options in the field are populated by a dynamic SOQL query in the controller, controlled by another selectlist (not multi-select). When the initial select list is chosen, the multi-select list is updated using a ajax and a partial page refresh.

 

I've just realised that I'm responding as two different profiles. I'm also "Big Ears", by the way....

 

Andy

 

bob_buzzardbob_buzzard

Yeah, I figured that bit out.  I'm also assuming you're not the Andy Rouse that used to race British touring cars :)

 

I've used the partial rerendering in the past to move values between multi-select picklists and I can't remember any issues of this nature.  

AndyRouseAndyRouse

I'm also not the famous wildlife photographer. Although, sometimes I pretend I am and see how long I can get away with it.

Big EarsBig Ears

Further update: It might not be the selection of multiple options, but the selection of particular options that causes it to break. It looks like I might have just been happening to select the "breaking" lines when selecting multiple lines. I'm still rooting around trying to find a cause.

 

Also - once the multi-select gets broken and becomes non-responsive, it looks like certain values "fix" it and get it working again? I'm trying to pin down whether those values are consistent when re-setting the controller, or if it changes from page load to page load.

 

Andy

bob_buzzardbob_buzzard

Do you have any embedded single quotes, odd characters or the like?

AndyRouseAndyRouse

I don't. The labels are alphanumeric and the values are ids. It's not something I've come across before.

 

Thanks for the incredibly quick response!

bob_buzzardbob_buzzard

This is bugging me too!  I've got a fair bit of code out there that does this sort of thing, so I'm keen to understand what might cause it to break.

AndyRouseAndyRouse

So - Minor update:

 

I added to the visualforce page, so that it would spit out the available options in the multi-select picklist as debug code on the page every time the user hit the "submit" button. It seems that, sometimes, the available options list drops one or more of the options (without the multi-select list being updated to reflect those options). If the user selects one of those "dropped" options, the controller doesn't know what to do and will not refresh properly.

 

The mystery now is: why would options get dropped? There's no refresh of the multi-select list going on when the user hits "submit", and the method that populates the list shouldn't return a different answer on each submit anyway.

 

I've tried to tackle it by adding a "persistence" to the multi-select list (it only repopulates the available options with the SOQL query if the controller finds it to be null), but that's throwing up a different problem. The list is always being returned as null, even if the populating routine is run. The debug log shows the correct values being assigned to the list and then the list being returned as null....

bob_buzzardbob_buzzard

So is the debug section something that is refreshed as part of the ajax rerendering?  Do you have debug on the controller side that shows what is happening during the getting of the values?

 

Can you post the code (or relevant snippets) - I've got lazy loading of selectlists in various places that haven't been throwing up these type of issues.

AndyRouseAndyRouse

Yup, I've got the debug section on the page re-rendering, so I can spot the values dropping out on re-render.

 

Here is the method that's called to populate the list options:

public list<selectOption> transactionHistoryProductOptions;
        
        public list<SelectOption> getTransactionHistoryProductOptions(){
            system.debug('GETTING THE OPTIONS');
            system.debug('THE OPTIONS ARE - '+transactionHistoryProductOptions);
            if(transactionHistoryProductOptions == null){
	            list<selectOption> transactionHistoryProductOptions = new list<selectOption>();
	            
	            ******
                     IN THIS SECTION THE CODE LOOKS AT ALL OF THE PRODUCTS THAT HAVE BEEN INVOLVED IN TRANSACTIONS IN THE LAST N DAYS (WHERE N IS SET BY THE USER). CODE THEN TURNS THOSE INTO A SET (SO THAT EACH PRODUCT IS ONLY REPRESENTED ONCE) AND THEN A LIST, SO IT CAN BE SORTED BY PRODUCT NAME IN ALPHABETICAL ORDER. THE CODE THEN ENDS UP WITH A LIST (CALLED "BLAH") OF STRINGS WHERE THE FORMAT IS "{!ProductName}HEREISTHEID{!ProductId}
                     ******
	            
	            for(string s : blah){
	            	list<string> blahlist = s.split('HEREISTHEID');
	            	transactionHistoryProductOptions.add(new selectOption(blahlist[1],blahlist[0]));
	            	system.debug('HERE IS THE LIST SO FAR: '+transactionHistoryProductOptions);
	            }
	            
	            system.debug('HERE IS THE LIST OF OPTIONS NOW = '+transactionHistoryProductOptions);
            }
				            
	   system.debug('HERE IS THE LIST OF OPTIONS - '+transactionHistoryProductOptions);
	   return transactionHistoryProductOptions;
            
        }

 

 

 In the debug log, the debug statements can see the transactionHistoryProductOptions list being assembled. Then, as the code leaves the "If" statement, the list apparently goes to null again before being returned.

 

I've also tried putting the return statement within the if statement and made it an "if-else" statement so that the list is either populated and returned, or just returned as-is. In this instance, the old problem returns of certain values being "dropped" when the user hits submit, even though the list should remain unchanged.

bob_buzzardbob_buzzard

This line:

 

list<selectOption> transactionHistoryProductOptions = new list<selectOption>();

 is declaring a local variable to the method within the scope of the if statement, which hides the controller property.  This is then discarded at the end of the if statement, and the null controller property is returned to the page.

 

If you change it to:

 

transactionHistoryProductOptions = new list<selectOption>();

 then you should be dealing with the property that the page is using.

This was selected as the best answer
AndyRouseAndyRouse

Oh my goodness. I am such an idiot. SUCH an idiot.

 

I didn't see it and the IDE usually fails to allow a save if I'm re-declaring a variable in that way.

 

So sorry to have caught up your time in this. Thanks for spotting it.

 

Andy