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
Eran VizelEran Vizel 

Portal login Apex code doesn't send password email

Hi all,

I'm trying to create a simple Customer Community login page on visualforce. Following the basic template given, I don't want the user to select his own password but receive an email with a random password for starters.

if ([SELECT Id FROM Contact WHERE Email = :email].size() > 0)
  {
   Contact existingCon = [SELECT Id, Account.Id FROM Contact WHERE Email = :email];
  
   String profileId = '00eD0000001PmG2'; // To be filled in by customer.
         //String roleEnum = null; // To be filled in by customer.
         String accountId = '0012000000OtpOj'; // To be filled in by customer.
        
         String userName = email;

         User u = new User();
         u.Username = userName;
         u.Email = email;
         u.FirstName = firstName;
         u.LastName = lastName;
         u.CommunityNickname = communityNickname;
         u.ProfileId = profileId;
   
   
   String userId = Site.createPortalUser(u, existingCon.Account.Id, null, true);
  
   ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Registration successful. Please check your email for password confirmation');
            ApexPages.addMessage(msg);
  
   return null;
  
  }


The user is being created and associated with the existing conact. However no password is being sent, although documentation on Site.createPortalSite states if a password is not send through the method (third parameter - null) and the boolean value is set to send a new member email (fourth parameter - true), an email is suppoused to be sent automatically.
Nothing is received. Please advise.

Thanks,
Eran
Ashish_SFDCAshish_SFDC
Hi Eran, 


Try this,

public class PasswordGenerator {
    public static String getPassword(Integer len) {
        Blob blobKey = crypto.generateAesKey(128);
        String key = EncodingUtil.convertToHex(blobKey);
       
        System.debug(key);
        return key.substring(0,len);
    }
   
    public static testMethod void testPG() {
        System.debug(PasswordGenerator.getPassword(8));
    }
}


Or , Use the code in the blog,

http://flexandsalesforce.blogspot.in/2011/07/generating-random-passwords-to.html

Controller:-

  public class PasswordGenerator
{
        public String comments { get; set; }
        public String EmployeeNumber { get; set; }
        public String company { get; set; }
        public String Qualification { get; set; }
        public String Email { get; set; }
        public String Name { get; set; }
         string strPassword ='';
       public List<SelectOption> getQual() {
        List<SelectOption> options = new List<SelectOption>();
       options.add(new SelectOption('--NONE--','------NONE-----'));
        options.add(new SelectOption('MBA','MBA'));
        options.add(new SelectOption('MCA','MCA'));
        options.add(new SelectOption('DEGREE','DEGREE'));
        options.add(new SelectOption('INTER','INTER'));
        options.add(new SelectOption('SCHOOLING','SCHOOLING'));
        return options;
        }
    public static String getPassword(Integer len)
     {
        Blob blobKey = crypto.generateAesKey(128);
        String key = EncodingUtil.convertToHex(blobKey);
        System.debug(key);
        return key.substring(0,len);
     }
   
     public pagereference save()
      {
        String password =PasswordGenerator.getPassword(12);
        Blog_Members__c obj=new Blog_Members__c();
        obj.Name=Name;
        obj.company__c=company ;
        obj.Email__c=Email;
        obj.Employee_Number__c=EmployeeNumber ;
        obj.Comments__c=comments;
        obj.Qualification__c=Qualification ;   
        obj.Password__c = password;
        insert obj;
        sendpassword(Email,password);
         ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Login Details has been mail to your Registered mail Address .'));
         company ='';
         Qualification ='';
         EmployeeNumber ='';
         comments ='';
         Email ='';
         Name ='';
         return null;
      }
     
      public void sendpassword(String Email,String Password)
      {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[]{Email};
        String[] ccAddresses = new String[] {'haribabu.amudalapalli@gmail.com'};
        String Password1 =Password;
        System.debug('hari Testing'+Password1+'Email address'+toAddresses );
        mail.setToAddresses(toAddresses);
        mail.setCcAddresses(ccAddresses);
        mail.setReplyTo('support@acme.com');
        mail.setSenderDisplayName('Salesforce Blog ');
        mail.setSubject('Pass Word From Salesforce Blog');
        mail.setBccSender(false);
        mail.setUseSignature(false);
        mail.setPlainTextBody('Your UserName: ' + Email +';/n Pass Word '+Password1 );
        mail.setHtmlBody('Your UserName: ' + Email +'\n Pass Word '+Password1);
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });    
      }
  }

Page:-

<apex:page showHeader="false" sidebar="false" Controller="PasswordGenerator" >
<apex:pagemessages />
<apex:form >
<apex:pageBlock tabStyle="Contact" >
<apex:pageBlockButtons location="Bottom">
<apex:commandButton value="Submit" action="{!save}" onclick="success();"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Salesforce Blog Registartion Form" >
Name:<apex:inputtext value="{!Name}"/><br/>
Email:<apex:inputtext value="{!Email}" id="email"/><br/>
Company:<apex:inputtext value="{!company}"/><br/>
EmployeeNumber:<apex:inputtext value="{!EmployeeNumber}"/><br/>
Comments about Blog:<apex:inputtextarea value="{!comments}" cols="27" rows="6"/><br/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>             
</apex:page>


Regards,
Ashish