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
hema k 29hema k 29 

How to write forgot password code apex and vf page in salesforce

NagendraNagendra (Salesforce Developers) 
Hi Hema,

Please find the snippet below.

Apex Controller:
/**
 * An apex page controller that exposes the site forgot password functionality
 */
public with sharing class ForgotPasswordController {
    public String username {get; set;}   

    public ForgotPasswordController() {}

    public PageReference forgotPassword() {
      boolean success = Site.forgotPassword(username);
      PageReference pr = Page.ForgotPasswordConfirm;
      pr.setRedirect(true);

      if (success) {        
        return pr;
      }
      return null;
    }
}
Visual Force Page:
<apex:define name="body">

<apex:form id="theForm">
<apex:pageMessages id="error"/>

<h2>Reset your password</h2>
<p>Forgot your password? Enter your email address below to send you a temprary password.</p>
<br/>

<div class="row">

  <div class="col-md-4 col-sm-12 col-md-offset-4">
      <div class="login-panel panel panel-default">
          <div class="panel-heading">
              <h3 class="panel-title"><strong>Forgot Password</strong></h3>
          </div>
          <div class="panel-body">
              <form role="form">
                  <fieldset>
                      <div class="form-group">
                          <label for="username">Username</label>
                          <apex:inputText required="true" styleClass="form-control" id="username" value="{!username}"/>
                      </div>
                      <!-- Change this to a button or input when using this as a form -->
                      <apex:commandButton id="submit" value="Reset Password" action="{!forgotPassword}" styleClass="btn btn-warning btn-lg btn-block" />
                  </fieldset>
              </form>
          </div>
      </div>
  </div>
</div>

</apex:form>                  
</apex:define>
</apex:composition>
</apex:page>
Hope this helps.

Kindly mark this as solved if the reply was helpful so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.

Thanks,
Nagendra