I have made a controller of self user registration . and while doing this what i am trying to make is that when ever user registered a password link should be sent on user email id ,just like when ever we create a org and sales force send a email verification link on our email id. Is it possible to do so?

public with sharing class CommunitiesSelfRegController {

    public String firstName {get; set;}
    public String lastName {get; set;}
    public String email {get; set;}
    public String password {get; set {password = value == null ? value : value.trim(); } }
    public String confirmPassword {get; set { confirmPassword = value == null ? value : value.trim(); } }
    public String communityNickname {get; set { communityNickname = value == null ? value : value.trim(); } }
    public String CompanyName { get; set; }
    public String fPhone { get; set; }

    public CommunitiesSelfRegController() {}

    private boolean isValidPassword() {
        return password == confirmPassword;

    public PageReference registerUser() {

        // it's okay if password is null - we'll send the user a random password in that case
        if (!isValidPassword()) {
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, Label.site.passwords_dont_match);
            return null;

         String profileId = '00e90000001jxLE'; // To be filled in by customer.
         String roleEnum = 'Partner User'; // To be filled in by customer.
         String accountId = ''; // To be filled in by customer.
         String CityOffice='00590000003BnUR';
         String StateOffice='00590000003BnSa';
         String Owner='00590000003Bn3L';   // sytem Admin ID 
         String AccountNumber;
         Account account= new Account();
         account.Name =firstName;
         account.SPBE__First_Name__c= firstName;
         account.SPBE__Last_Name__c = lastName;        
         insert account;         

        // account.isPartner=true;

         update account;
        accountId = account.Id;
        String userName = email;
        User u = new User();
        u.Username = userName;
        u.Email = email;
        u.FirstName = firstName;
        u.LastName = lastName;
        u.CommunityNickname = firstName+AccountNumber;
        u.ProfileId = profileId;

        String userId = Site.createPortalUser(u, accountId, password); 

        if (userId != null) { 
            if (password != null && password.length() > 1) {
                return Site.login(userName, password, ApexPages.currentPage().getParameters().get('startURL'));
            else {
                PageReference page = System.Page.CommunitiesSelfRegConfirm;
                return page;
        return null;

Have you configured any email template for this mail? Make sure email template needs to include this special merge field


| improve this answer | |
  • Can you explain your answer? What this expression will do? – LetMeCodeYou Aug 16 '18 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.