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
ManDarMan2ManDarMan2 

new user email not getting sent

I have a customer portal which is self-registration enabled. Self-registration happens through Sites. I have setup SiteLogin and SiteRegistration apex classes with some minor changes. The user (and contact) gets created, but the "new user" email is never sent out, even though I am setting the triggerUserEmail option. Here's the code snippet:

 

//Async method, using @future, to create the User record and associate it to the previously created Contact
//This uses @future because you can not have mixed DML operations for standard objects (Account, Contact) and Setup objects(User)
@future static void createUser(String contactId, String userName, String firstName, String lastName, String email, String phone, String zip, String age, String nickName, String profileId) {
       
        System.debug('nickname = ' + nickName + ' phone = ' + phone + ' zip = ' + zip + ' age = ' + age + ' contactid = ' + contactId + ' email = ' + email + ' firstName = ' + firstName + ' lastName = ' + lastName + ' userName = ' + userName + ' profileId = ' + profileId);
       
        Database.DMLOptions dmo = new Database.DMLOptions();
        dmo.EmailHeader.triggerUserEmail = true;
       
        User u1 = new User(contactId=contactId, username=userName, firstname=firstName,
            lastname=lastName, email=email, phone=phone, postalcode=zip, zip__c=zip, age_group__c=age,
            alias = nickName, profileid = profileId, emailencodingkey='UTF-8',
            languagelocalekey='en_US', localesidkey='en_US', timezonesidkey='America/Los_Angeles',
            isportalselfregistered=true
            );
     
        u1.setOptions(dmo);
        insert u1;
    }

 

Again, the user and contact get created just fine, but the email with password is never sent. (The "reset password" email does get sent.) The email template does not use any merge variables.

 

Is there some other setting that I am missing? Is this a Sites issue or a Customer Portal Issue?

 

Best Answer chosen by Admin (Salesforce Developers) 
BulentBulent
You need to use the Site.createPortalUser method in order this logic to work

All Answers

BulentBulent

do you expose password field on your site registration page?

after registration do you see the registration confirmation page or user gets authenticated to the portal? 

ManDarMan2ManDarMan2

I do not expose the password field on the registration page.

After registration, the registration confirmation page is show. The user does NOT get authenticated to the portal. (They are to wait for the password in the "new user" email then click on the SiteLogin link.)

 

Thanks. 

BulentBulent
if you send me your org id and the site name via email, I can take a look at it.
BulentBulent
You need to use the Site.createPortalUser method in order this logic to work
This was selected as the best answer
FadiShamiFadiShami

I have the same problem, but i am using Site.createPortalUser- 

 

I have a custom visualforce email template that is being used to send the registration email though, not the out of the box email.

 

Thanks,

Fadi

EIE50EIE50

Hi fadi,

 

I am also having a problem where no emails are being sent when a new user registers with my ideas only portal site. Where do i get the site.createportaluser method?

BulentBulent

take a look at the out of the box siteregistration class.

If you are using this class then there a re two flows:

1- if you pass password then user gets created and authenticated, no email is sent

2- if you are not passing password then user gets created no authentication, and user get the email with a temp password (based on the new user email template defined on the portal setup page), then user logs in with the tempo password, changes the password 

FadiShamiFadiShami

I noticed that behavior, what i do not get it why not send and email even though a password has been passed !

Most of users do want a confirmation email upon registration. I think an email should be optional in the first scenario, and give the admin the ability to turn on/off.

 

 

EIE50EIE50

Hi,

 

I am just using the standard functinalilty. Yes i do use SiteRegistercontroller. Now what am i suppose to change so that users can get an email which contains the link for the site and username & password.

 

Also, i dont want a same user to register twice using different names but with same email id. How can i make email id a unique field? Pls guide me.

EIE50EIE50

Also, below is the code for my siteregistercontroller.  


public class SiteRegisterController {

    // PORTAL_ACCOUNT_ID is the account on which the contact will be created on and then enabled as a portal user.

    // you need to add the account owner into the role hierarchy before this will work - please see Customer Portal Setup help for more information.       

    private static Id PORTAL_ACCOUNT_ID = '001Q00000080QYz';

    

    public SiteRegisterController () {

    }



    public String username {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(); } }

      

    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);

            ApexPages.addMessage(msg);

            return null;

        }    

        User u = new User();

        u.Username = username;

        u.Email = email;

        u.CommunityNickname = communityNickname;

        

        String accountId = PORTAL_ACCOUNT_ID;



        // lastName is a required field on user, but if it isn't specified, we'll default it to the username

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

        if (userId != null) { 

            if (password != null && password.length() > 1) {

            String startUrl = System.currentPageReference().getParameters().get('startUrl');

              if (startUrl == null) startURL = (Site.getPrefix() == null ? '' : Site.getPrefix()) + '/home/home.jsp';

              return Site.login(username, password, startURL);

          

            }

            else {

                PageReference page = System.Page.SiteRegisterConfirm;

                page.setRedirect(true);

                return page;

            }

        }



        return null;

    }

FadiShamiFadiShami

Emails should never, and won't contain passwords - The thing that can be sent directly from SFDC , not manual, is a temp password when a user's password gets reset.

 

To make email unique, you could probably query for that email in your controller to see if a someone else exists with the same email address, etc ....

 

An easy way to do it , is to force your username be the same as ur email, this way if someone want to register more than once, they will have to provide a different email address..

 

Hope this helps...

BulentBulent

this section of the online help explains the process

 

The built-in registration process:

    • Checks new user information against existing users for the Customer Portal associated with the site
    • Checks to see if a contact already exists for the new user
    • Creates a new contact, if one doesn't already exist, and associates it with the account for the site
     
    email is used as a user name.
     
    you don't need to update the apex class in order to choose send email or not. You need to update your registration visualforce page so that you don't capture/pass password to the class.
     
    however, if you want to sent an email regardless of the flow then you can write a trigger on the user object and if the user is a portal user you send an email from apex. 
    EIE50EIE50

    Hi,

     

    I have modified the site register visualforce page and now an email with login id and temp pass is being sent. Now my question is, while registering i have username, nickname and email id on the site register page, i dont want users to register themselves with different names for a same email id. How could it be done?

     

    Thanks

    BulentBulent

    use email address as the user name. User name is unique for a given portal

     

    EIE50EIE50
    Sorry, but how do i do that? In visualforce page or apex?
    FadiShamiFadiShami
    in VF only display one field : email, then in ur apex class tie it to username and email on the user object
    EIE50EIE50

    This is my visualforce page for site register. all i am asking here for is email id, but its giving me this error.

     

    New User Registration
    ERROR

    Error:

     That username is invalid. A username can only contain alphanumeric characters.
      

    visualforce page:

     

    <apex:page id="Registration" showHeader="true" sidebar="false" controller="SiteRegisterController" title="{!$Label.site.register}">
     
        <apex:define name="body"> 
          <center>
            <apex:panelGrid bgcolor="white" columns="1">
              <br/>
              <br/>
              <apex:panelGrid width="758" cellpadding="0" cellspacing="0" bgcolor="white" columns="1" styleClass="topPanelContainer">
                <br/>
                <apex:outputPanel layout="block" styleClass="topPanel">
                  <apex:panelGrid width="758" cellpadding="0" cellspacing="0" bgcolor="white" columns="2">
                    <apex:image url="{!URLFOR($Resource.SiteSamples, 'img/clock.png')}"/>
                    <apex:panelGroup >
                      <br/>
                      <apex:outputText styleClass="title" value="{!$Label.site.user_registration}"/>
                      <br/>
                      <apex:form id="theForm" forceSSL="true">
                        <apex:pageMessages id="error"/>
                        <apex:panelGrid columns="2" style="margin-top:1em;">
                                                <apex:outputLabel value="{!$Label.site.email}" for="email"/>
                          <apex:inputText required="true" id="email" required="true" value="{!email}"/>
                         
                          <apex:commandButton action="{!registerUser}" value="{!$Label.site.submit}" id="submit"/>
                        </apex:panelGrid>
                        </apex:form>                 
                      <br/>
                    </apex:panelGroup>
                  </apex:panelGrid>
                 </apex:outputPanel>
               
              </apex:panelGrid>
           </apex:panelGrid>
          </center>
          <br/>
        </apex:define>
     
    </apex:page>

     

     

    And my apex class for this controller is:

     

    public class SiteRegisterController {

        // PORTAL_ACCOUNT_ID is the account on which the contact will be created on and then enabled as a portal user.

        // you need to add the account owner into the role hierarchy before this will work - please see Customer Portal Setup help for more information.       

        private static Id PORTAL_ACCOUNT_ID = '001Q00000080QYz';

        

        public SiteRegisterController () {

        }



        public String username {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(); } }

          

        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);

                ApexPages.addMessage(msg);

                return null;

            }    

            User u = new User();

            u.Username = username;

            u.Email = email;

            u.CommunityNickname = communityNickname;

            

            String accountId = PORTAL_ACCOUNT_ID;



            // lastName is a required field on user, but if it isn't specified, we'll default it to the username

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

            if (userId != null) { 

                if (password != null && password.length() > 1) {

                String startUrl = System.currentPageReference().getParameters().get('startUrl');

                  if (startUrl == null) startURL = (Site.getPrefix() == null ? '' : Site.getPrefix()) + '/home/home.jsp';

                  return Site.login(username, password, startURL);

              

                }

                else {

                    PageReference page = System.Page.SiteRegisterConfirm;

                    page.setRedirect(true);

                    return page;

                }

            }



            return null;

        }

     

    What should i modify?

     

    thanks

    FadiShamiFadiShami

    try

    u.Username = email 

    EIE50EIE50
    Now it says Invalid email address, if i try doing u.usrename =email
    EIE50EIE50

    Got it guys...

     

    Thanks

     

     

    IvanMIvanM

    Hi! I have the same problem, but i am using Site.createPortalUse in @future method. Here is my method, which I modified:

     

    @future static void createUser(String contactId, String accountId, String email, String firstName, String lastName, String userName, String profileId,
        String city, String postalCode, String street, String country, String state, String phone, String fax, Boolean isActive) {


            Database.DMLOptions dmo = new Database.DMLOptions();
            dmo.EmailHeader.triggerUserEmail = true;
            User newU = new User(alias = 'standt', email=email,
                emailencodingkey='UTF-8', firstname=firstname, lastname=lastname, languagelocalekey='en_US',
                localesidkey='en_US', profileid = profileId, contactId=contactId,
                timezonesidkey='America/Los_Angeles', username=username,
                city=city, postalCode=postalCode,
                street=street, country=country, state=state, phone=phone, fax=fax, isActive=isActive);
            newU.setOptions(dmo);
            String userId = Site.createPortalUser(newU, accountId, null);
        }

     

    It doesn't create new user. May be I missed something, or do I need some specific settings?

     

    Thanks.