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
rajesh k 10rajesh k 10 

How to write forgot password code?

Hi,
        I created one login page.In this login page I gave one link as forgot password.When urer user click this link ,link goto another visualforce page.In this page i gave Username text box .When user enter username that username match to my object record user means sent email to password and display message as ur password sent to email otherwise display an error how?

please help me.......


AshlekhAshlekh
Hi,

If you are working on Custmer Community and want to provide forget password functionality then you can use forgotPassword(String) method of Site class.

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_sites.htm#apex_System_Site_forgotPassword
rajesh k 10rajesh k 10
Hi Aslekh,
                    I am working on sample login ,My login credentials are there in Myobject__c
forgot password like below ,but i gave one username that username is not there in my object.In my class not through an error page redirect to same page,

My class like below:

public class SendEmailController {

    public String username { get; set; }
  
    public List<Myobject__c> lstcon{get;set;}
  
    public SendEmailController()
    {
    lstcon=new List<Myobject__c>();
    }

    public PageReference doSubmit() {
  
     lstcon=[SELECT username__C,Password__C,Email FROM Myobject__c where Username__C =:username];


    for(Myobject__c c:lstcon)
    {

        if(username == c.Username__C)
        {
        String LoginURL='https://c.ap1.visual.force.com/apex/loginPage';
      
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    
            
    
              mail.setToAddresses(new String[] {c.Email});
            
                           
              mail.setPlainTextBody(
              
                  ' Please use your Username and the  password, listed below, to login. \n\n' +
                  'User Name: ' +c.username__C + ' \n' +
                  '(Temporary or Set) Password: ' + c.Password__c + ' \n' +
                  'Login Link: ' + LoginURL
               
              );
            
              // send email
              Messaging.SendEmailResult[] emailresult = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
              system.debug(emailresult);
            
          }
       
        
    }

        return null;
    }


}

when unauthorised user enter username I want to through an error.but above code not throughing error other wise page reference to success

plese help me.....
AshlekhAshlekh
Hi.

Where have you written code which through the error, And if  I am not wrong your are creating your own login functionality and will provide this to use  by site.
rajesh k 10rajesh k 10
Hi,
        I added in above code 

public class SendEmailController {

    public String username { get; set; }
 
    public List<Myobject__c> lstcon{get;set;}
 
    public SendEmailController()
    {
    lstcon=new List<Myobject__c>();
    }

    public PageReference doSubmit() {
 
     lstcon=[SELECT username__C,Password__C,Email FROM Myobject__c where Username__C =:username];


    for(Myobject__c c:lstcon)
    {

try{
        if(username == c.Username__C)
        {
        String LoginURL='https://c.ap1.visual.force.com/apex/loginPage';
     
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
   
           
   
              mail.setToAddresses(new String[] {c.Email});
           
                          
              mail.setPlainTextBody(
             
                  ' Please use your Username and the  password, listed below, to login. \n\n' +
                  'User Name: ' +c.username__C + ' \n' +
                  '(Temporary or Set) Password: ' + c.Password__c + ' \n' +
                  'Login Link: ' + LoginURL
              
              );
           
              // send email
              Messaging.SendEmailResult[] emailresult = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
              system.debug(emailresult);
           
          }
      }catch(Exception e)
           {    
          
           ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Fatal,'No Record Exists in our database...');
            ApexPages.addMessage(msg);
            return null;
          }  
    }

        return null;
    }


}

But there is no errror
Kumari PurnimaKumari Purnima
Dear Rajesh,

Your above code is having no error. But when you execute this code then it will through an exception. 
mail.setToAddresses(new String[] {c.Email});----> This line throws an exception.

 public void forgetpassword()
    {
    List<Registration__c> reglist = new List<Registration__c>();
    try
    {
    if(!String.isBlank(username))
    {
        reglist=[SELECT username__c,Password__c, Email_ID__c FROM Registration__c where Username__c =:username];
        for(Registration__c c:reglist)
        {
                if(username == c.Username__c)
                {
                        String LoginURL='http://purnimacompnova-developer-edition.ap2.force.com/DigitalCampus';
                        String str;
                        str = c.Email_ID__c;
                        String[] toAddresses = new String[] {str};
                        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                        mail.setSenderDisplayName('Salesforce Admin');
                        mail.setSubject('Login URL and Password');
                        mail.setBccSender(false);  
                        mail.setUseSignature(false);
                        mail.setToAddresses(toAddresses);
           
                          
                        mail.setPlainTextBody(
             
                        ' Please use your Username and the  password, listed below, to login. \n\n' +
                        'User Name: ' +c.username__c + ' \n' +
                        '(Temporary or Set) Password: ' + c.Password__c + ' \n' +
                        'Login Link: ' + LoginURL
              
                        );
           
                        // send email
                      Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });  
           
                }
                else{
                     ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'No Record exists in our database'));
                }
            }
         }
         else
        {
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please enter your username'));
        }
    }
      catch(Exception e)
      {    
          
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Fatal,'No Record');
            ApexPages.addMessage(msg);
            //return null;
      }
    
    //return null;
 }

I modified you code and it is working fine. Hope it will help you.