function readOnly(count){ }
Don't have an account?
Search for an answer or ask a question of the zone or Customer Support.
You need to sign in to do that
Sign in to start searching questions
Signup for a Developer Edition
Sign in to start a discussion
how to create user through apex code.
Hi Kiran,
we can create user through apex. here is the code.
User u = new user(); u.LastName = 'Test Code'; u.Email = 'test@test.com'; u.Alias = 'Tcode'; u.Username = 'test1234444@test.com'; u.CommunityNickname = 'test12'; u.LocaleSidKey = 'en_US'; u.TimeZoneSidKey = 'GMT'; u.ProfileID = '00e90000000oyi5'; u.LanguageLocaleKey = 'en_US'; u.EmailEncodingKey = 'UTF-8'; insert u;
Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
Thanks,
Hitesh Patel
Profile profileId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1]; User usr = new User(LastName = 'LIVESTON', FirstName='JASON', Alias = 'jliv', Email = 'jason.liveston@asdf.com', Username = 'jason.liveston@asdf.com', ProfileId = profileId.id, TimeZoneSidKey = 'GMT', LanguageLocaleKey = 'en_US', EmailEncodingKey = 'UTF-8', LocaleSidKey = 'en_US' );
Hi Kiran,
we can create user through apex. here is the code.
Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
Thanks,
Hitesh Patel
You can try below, Hope it will work
Profile profId = [select Id from Profile where name=:'Standard User' limit 1];
String orgId = UserInfo.getOrganizationId();
String dateString = String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','');
Integer randomInt = Integer.valueOf(math.rint(math.random()*1000000));
String uniqueName = orgId + dateString + randomInt;
if(uniqueName.length()>51)
{
uniqueName=uniqueName.substring(0,51);
}
User tuser = new User( firstname = 'Test',
lastName = 'Kiran',
email = uniqueName + '@test' + orgId + '.org',
Username = uniqueName + '@test' + orgId + '.org',
EmailEncodingKey = 'ISO-8859-1',
Alias = uniqueName.substring(18, 23),
TimeZoneSidKey = 'America/Los_Angeles',
LocaleSidKey = 'en_US',
LanguageLocaleKey = 'en_US',
ProfileId = profId.Id);
insert tuser;
Thanks,
Regards,
Sabir
Below Sample code can fulfill your requirements. Hope this will work for you.
/* Fill all mandatory fields */
user.Username = 'test@samp.com';
user.Email = 'test@samp.com';
user.Alias = 'tsamp';
user.UserRoleId = 'user_role_id_goes_here';
user.ProfileId = 'user_profile_id_goes_here';
user.Employment_Start_Date__c = '10/01/2018';
user.IsActive = false; // here we are creating inactive user account
try
{
insert user; // insert the user record
}
catch(Exception e)
{
System.debug(e);
}
Please mark this as best answer if this solves your problem.
Thank you,
Ajay Dubedi