1

I'm having an issue when creating an user from the apex code. My code do the following:

    @future
    public static void createUser(Id contactId) {
        Contact contact = [SELECT Id, Email, FirstName, LastName, AccountId FROM Contact WHERE Id = :contactId];
        Profile profile = [SELECT Id FROM Profile WHERE Name = 'My Community Profile'];
        
        User newUser = new User(
            Alias=getAlias(contact),
            Email=contact.Email,
            EmailEncodingKey='UTF-8',
            FirstName=contact.FirstName,
            LastName=contact.LastName,
            LanguageLocaleKey='en_US',
            LocaleSidKey='en_US',
            contactId=contact.Id,
            TimeZoneSidKey='America/New_York',
            Username=contact.Email,
            CommunityNickname=getNick(contact),
            IsActive=true,
            ProfileId=profile.Id
        );

        Database.DMLOptions dmo = new Database.DMLOptions();
        dmo.EmailHeader.triggerUserEmail = true;       
        dmo.EmailHeader.triggerOtherEmail = true;
        dmo.EmailHeader.triggerAutoResponseEmail = true;       
        dmo.optAllOrNone = false;

        newUser.setOptions(dmo);
        insert newUser;
    }

On my digital experience site:

  • I have the My Community Profile on the Members/Selected Profiles option
  • I also have the option Send welcome email checked on the Emails section along with the template.
  • I have a dedicated template for the new member welcome.
  • The site is active.

After I run the code, I can see the user being created with the right profile. But the welcome message is not being sent.

What am I missing?

3
  • 1
    And this profile is assigned to the experience site? Does the site have the welcome email template selected?
    – Damecek
    Mar 11, 2022 at 8:03
  • yes, it's assigned and yes, the email template is selected Mar 11, 2022 at 13:35
  • when you say that the profile is assigned to the experience site, you mean on the Members section, right ? if so, yes, it's assigned. Mar 11, 2022 at 13:37

1 Answer 1

2

The reason why it wasn't working was because we need to give All Email option to the Access Level on deliverability

enter image description here

Now, it's working perfectly.

1
  • 1
    Welcome to the Salesforce-specific Stack Exchange. We've all done that before (not setting it to All email)! Thanks for coming back to add the answer, as that could help future readers.
    – Moonpie
    Mar 11, 2022 at 14:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .