4

When using the Saleseforce UI, it is possible to change a user's email and send them a password at the same time by changing the email and clicking 'Generate new password and notify user immediately' checkbox before saving. This avoids sending 2 emails (one for email change and one for password reset).

Is there a similar way to do this using Apex code that could be run in a SandboxPostCopy class?

I know it is possible to reset a user's password using System.resetPassword(userId, true);. I'm not sure how to go about doing the email update at the same time to avoid sending the password reset to the wrong email.

2 Answers 2

3

No, at least not in my attempts at this.

The methods to reset or set a password for a user immediately acts. There's no way to chain it with the update of the user's email.

Since they're separate transactions, the change of the user's email will always result in an email being sent to confirm this change regardless if you then call the reset or setPassword method.

Like you noted, even if you change the email on the user first, the password reset email will go to the original email as the change has not been confirmed.

0

For the email reset:

User Utente = [Select Id, email,isactive from user where email = '[email protected]' limit 1];
Utente.email = '[email protected]';
update Utente;

Database.DMLOptions dlo = new Database.DMLOptions();
dlo.EmailHeader.triggerUserEmail = true;
database.update(Utente, dlo);
1
  • Welcome to Salesforce Stack Exchange (SFSE)! Thank you for sharing code, but please edit your answer to add additional information to explain how the code you posted does not run into the issues mentioned in both the original question and in the other answer. (Those issues including that if you change the email first, the password reset email will still go to the old email since the email change has not yet been confirmed by the user.) At SFSE, explaining "how" or "why" is as (or more) important than the solution itself.
    – Moonpie
    Jan 11, 2022 at 11:44

You must log in to answer this question.

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