13

I have a method that creates a new user and sets a custom password, everything works fine to the point where I try to log in with the new user and get an error that the login details are incorrect. Even the login history for that user does not show any entry stating that the password was incorrect, etc.

I have verified that the username and password both are correct, and it works when I do a subsequent setPassword() using the console.

Example:

User newUser = New User();
//...set user fields
insert newUser;    

String password = getPassword(8);
system.setPassword(newUser.Id, password);
//...email user the password

When I run this in the console the same problem occurs:

User u = new User();

u.username = '[email protected]';
u.email = '[email protected]';
u.lastName = 'test';
u.firstName = 'test';
u.alias = 'test1236';
u.languagelocalekey = 'en_US';
u.localesidkey = UserInfo.getLocale();
u.emailEncodingKey = 'UTF-8';
u.timeZoneSidKey = 'America/Los_Angeles';
u.profileId = '00eG0000000FgyJ';
insert u;

system.setPassword(u.id, 'testpassword1');

*UPDATE

I can login after 5 minutes or so after the user is created, I have tried on two different orgs.

13
  • Does the debug log show any errors? Commented Jan 9, 2013 at 17:36
  • 2
    I recreated and did not have this problem. Is this code inside a try block that might be hiding an error?
    – zjeh
    Commented Jan 9, 2013 at 17:40
  • 1
    Are you trying to login to test.salesforce.com or login.salesforce.com. If you are in a sandbox you need test.salesforce.com. Commented Jan 9, 2013 at 17:42
  • Nope, no errors and this is in a dev org. The user is created and active this is what I have used in the console: User u = new User(); u.username = '[email protected]'; u.email = '[email protected]'; u.lastName = 'test'; u.firstName = 'test'; u.alias = 'test1236'; u.languagelocalekey = 'en_US'; u.localesidkey = UserInfo.getLocale(); u.emailEncodingKey = 'UTF-8'; u.timeZoneSidKey = 'America/Los_Angeles'; u.profileId = '00eG0000000FgyJ'; insert u; system.setPassword(u.id, 'testtest1');
    – rumdumdum
    Commented Jan 9, 2013 at 20:20
  • 1
    And I think it goes without saying that you should deactivate that user since you posted real credentials. :-) Commented Jan 9, 2013 at 20:26

1 Answer 1

11

The authentication mechanism seems to have a replication delay. Changing your user name results in an email being sent to you:

Dear Mr. User,

Your username has changed.

New username: ABC

Old username: XYZ

The new username may take several minutes to become fully functional. We recommend you log in with the following link for the next 24 hours to avoid problems.

https://na1.salesforce.com/?c=...

If you can't click the link, please copy and paste it into your browser.

Thank you, salesforce.com

I suspect that this same issue occurs when creating a new user; the user is only able to log in immediately because of the magic link they are sent in a new user email. When creating the user through other means, they will most likely experience a delay before they are able to log in using the provided user name.

You must log in to answer this question.

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