+ Start a Discussion
praveen murugesanpraveen murugesan 

User is not receiving an email in one org but not receiving in another org

Hi All,

 I am able to create a new user through apex code but the user is not receiving an password confirmation email in one org. But I have used same code in another org in that user is receiving mail perfectly.   Any suggestions??

I have used this code 

Database.DMLOptions dmlo = new Database.DMLOptions();
dmlo.EmailHeader.triggerUserEmail = true;
dmlo.EmailHeader.triggerAutoResponseEmail= true;
insert user;

Is that org sending any email out? Askign this questions as there is a setting under Setup > Email Administration > Deliverability which when setup as No Access restricts all emails being sent out from the ORG:


Please check.. 


Please check if the email is being sent or not from the code, for this check debug logs and email logs from setup for this user.

Setup|Administration setup|Monitoring|Debug logs/Email log files

Also verify the email settings from the org where it is working.
praveen murugesanpraveen murugesan

Thanks for your time I have checked the access level is All email only.
praveen murugesanpraveen murugesan

I have checked in both debug log and email log and I couldn't find any error. 

And if i checks Generate password and notify to user check box in user detail page(user which i have created using apex) email is sending properly.

While creating new user using apex only it is not sending.
Hi Praveen,

Can you test if this works:

User l = new User(alias = 'newu', email=email,
emailencodingkey='UTF-8', lastname=lastname, languagelocalekey ='en_US',
localesidkey='en_US', profileid = profileId, contactId=contactId,
timezonesidkey='America/Los_Angeles', username=username);

Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule = true;
dmo.EmailHeader.triggerUserEmail = true;
Database.insert (l, dmo);
praveen murugesanpraveen murugesan

It is not working. I am inserting only user record. I think no need to use assignmentRuleHeader. This are the data i am using to create a record 

User:{Phone=34543432, TimeZoneSidKey=Europe/London, UserPermissionsSFContentUser=true, LanguageLocaleKey=en_US, UserPreferencesContentEmailAsAndWhen=true, ProfileId=00eb0000000ct9oAAA, LocaleSidKey=en_GB, LastName=Saravanan, UserPreferencesContentNoEmail=true, EmailEncodingKey=ISO-8859-1, Email=pra--nm@---.com, Alias=TeS, Username=pra--ms@----.com, FirstName=Test, MobilePhone=898888665}