Skip to content

Instantly share code, notes, and snippets.

@Nahumancer
Created April 19, 2019 19:37
Show Gist options
  • Save Nahumancer/3ebb182b3be35331f7cc7dc2331a0eb1 to your computer and use it in GitHub Desktop.
Save Nahumancer/3ebb182b3be35331f7cc7dc2331a0eb1 to your computer and use it in GitHub Desktop.
Test User creation
public static void generateTestUser(String randomUserName){
System.runAs ( new User(Id = UserInfo.getUserId()) ) {
Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
User auxTestUser = new User();
auxTestUser.Alias = 'standt';
auxTestUser.Email = 'testemail@org.com';
auxTestUser.EmailEncodingKey = 'UTF-8';
auxTestUser.LastName = 'Test';
auxTestUser.LanguageLocaleKey = 'en_US';
auxTestUser.LocaleSidKey = 'en_US';
auxTestUser.ProfileId = p.Id;
auxTestUser.TimeZoneSidKey = 'America/Los_Angeles';
auxTestUser.Username = randomUserName;
insert auxTestUser;
}
}
@Nahumancer
Copy link
Author

To be used like this:

String randomUserName = 'TestUser@' + String.valueOf(System.currentTimeMillis()) + '.com';
generateTestUser(randomUserName);
ID ownerUserID = [SELECT ID FROM User WHERE Username = :randomUserName LIMIT 1][0].ID;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment