Friday, March 18, 2016

ActionFunction is not calling the actionmethod on Visualforce Page: Issue, Approach and Solution

Hi All,

Recently I faced an issue at the time of developing a dynamic and advanced visualforce page with a custom template and standardstylesheets=”false”. By the way these parameters were not related to the issue but I am just mentioning my implementation in consideration.

The Scenario is, I am having a command button or html button, onclick of that I am calling a js function and from that js function I am doing something on the visualforce page like setting value of some DOM Component and styling etc… After that I am calling my actionfunction to call an Action Method which is connected to a function in my Apex Controller.

Code Example:

actionstatus id="status1" startText="Processing..." 

actionFunction name="saveRecord" status="status1" action="{!SaveRecord}" reRender="errMsg" 

HTML Button tag with type="button" and onclick="saveRecord (); return false;"
OR commandButton onclick=" saveRecord (); return false;

The Issues is, onclick of the button my js function is getting called, from that js function my actionfunction is getting called instead of that reloading the page. I solved the reloading by putting return false on my button at the time of calling the javascript function. I could see the status in my UI but that actionfunction still not calling the actionmethod. I am able to see the console logs before calling the actionfunction and after calling the actionfunction with no js error. I am able to see the actionstatus but there is no entry in the debug log for my save method call. I tried by replacing the apex function by thinking may be some error in apex function which is doing something wrong but nothing worked.

Strange was I haven’t found specific solution for this issue in my search anywhere so I applied the following approach to find the cause of issue.

Approach to find the cause: I removed all my visualforce code and placed one section by another and found that in a Selectlist I directly reference a lookup field in value parameter, when I was placing that portion, my button is not working.

Solution: I removed that direct reference and used a string parameter instead. Onchange of that selectlist I populated the value of the string to the lookup field and my button starts working, means the actionfunction is calling the action method now.

Suggestion: Directly referencing the lookup field or Id on the visualforce page could cause unexpected behavior in your functionality sometime not all the time so try avoiding that.

Thanks,
Amit Goyal
Email: amitgoyal09@gmail.com
Skype: amitgoyal09

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.