Skip to main content

Code reviews rule: User password set programmatically

Written by David Martin
Updated this week

User password set programmatically

Why is this an issue?

Setting user passwords programmatically using System.setPassword() bypasses Salesforce's secure password management features. This practice:

  • May result in weak or predictable passwords

  • Bypasses password history and complexity requirements

  • Creates security audit concerns

  • Can expose passwords in code or logs

Salesforce provides secure, platform-managed password flows that should be used instead.

Examples

Example of incorrect code:

public void createUserWithPassword(String username, String password) {
User u = new User(/* user fields */);
insert u;
System.setPassword(u.Id, password);
}

Example of correct code:

public void createUser(String username) {
User u = new User(/* user fields */);
insert u;
// Let Salesforce send a password reset email
System.resetPassword(u.Id, true);
}

How can I fix violations?

  1. Use System.resetPassword(): This sends a secure password reset email to the user, allowing them to set their own password.

  2. Use SSO: Implement Single Sign-On to eliminate the need for Salesforce-managed passwords.

  3. Use Identity Connect: For user provisioning scenarios, use Salesforce Identity Connect or similar identity management solutions.

Resources

Did this answer your question?