0

I have a command button that calls an actionfunction to reset a password. I then want to alert the user of their new password. In the controller the new password is not being saved.

VisualForce:

<apex:commandButton value="Reset Password" onclick="resetPassword; alert({!newPassword});"/>
<apex:actionFunction name="resetPassword" action="{!setnewPassword}"/>

Controller:

public String newPassword {get; set;}

public String setnewPassword() {
    this.newPassword = 'test';
}

1 Answer 1

1

Your onclick is incorrect; you're not actually calling resetPassword. That should look more like this:

onclick="resetPassword(); alert('{!JSINHTMLENCODE(newPassword)}');"

However, that still won't show the new password correctly, because the onClick will finish before the resetPassword function completes (it's asynchronous). So, you'd want to move the alert to the actionFunction:

<apex:actionFunction id="resetPassword" name="resetPassword" action="{!setNewPassword}" 
          oncomplete="alert('{!JSINHTMLENCODE(newPassword)}');" reRender="resetPassword" />
2
  • This gives me a page error of "Formula Expression is required on the action attributes." @sfdcfox Aug 7, 2015 at 18:12
  • Should be working. It is clearly an expression. On the other hand, I'd avoid using actionFunction methods for this purpose. It should be just as easy to bind the setNewPassword function directly to the commandButton, then move the oncomplete to the commandButton.
    – sfdcfox
    Aug 7, 2015 at 19:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .