0

I'm trying to test a scheduled Apex class and I have a method that generates fakes users within my test class:

List<User> newUsers = new List<User>();
Profile p = [
    SELECT Id
    FROM Profile
    WHERE Name='System Administrator'
];

for (Integer i = 0; i < 20; i++) {
String firstName = (i < 10) ? 'Should Be Deleted' : 'Should Be Active';
String userType = (i < 10) ? 'CSPLitePortal' : 'Standard';
User u = new User(
    IsActive=true,
    FirstName=firstName,
    LastName=String.valueOf(i),
    Username='test' + i + '@wefowqijefwqofijwfowifjwefoij.com',
    Email='test' + i + '@wefowqijefwqofijwfowifjwefoij.com',
    Alias='test' + i,
    TimeZoneSidKey='America/Denver',
    LocaleSidKey='en_US',
    EmailEncodingKey='UTF-8',
    LanguageLocaleKey='en_US',
    ProfileId=p.Id,
    UserType=userType //Error is here
);

I am getting the lint error "Field is not writeable: User.UserType" here. I need to be able to set the UserType for what my scheduled Apex does. Is there any way to set/modify the User.UserType field?

1

It looks like you're trying to create some kind of community user like a Customer Community users. Community users require an existing Contact and Account record, and the user record has to be linked to the contact record.

You can use Site.createExternalUser() to create a community user - this will automatically create the contact record. See documentation.

If this method doesn't meet your requirements, then you would have to create an Account and Contact first, then create your community user. In that case you must set ContactId in the user and do not try to set the UserType.

Account acct = new Account(Name = 'Apex Test');
insert acct;
Contact con = new Contact(LastName = 'Apex Test', AccountId = acct.Id);
insert con;
User u = new User(
    IsActive=true,
    FirstName=con.FirstName,
    LastName=con.LastName,
    Username='test' + i + '@wefowqijefwqofijwfowifjwefoij.com',
    Email='test' + i + '@wefowqijefwqofijwfowifjwefoij.com',
    Alias='test' + i,
    TimeZoneSidKey='America/Denver',
    LocaleSidKey='en_US',
    EmailEncodingKey='UTF-8',
    LanguageLocaleKey='en_US',
    ProfileId=p.Id,
    ContactId = con.Id
);
1

The UserType field is not a settable field. It will be derived from the license applied to the user when setting the profile.

Given you're setting the profile to be the system administrator profile, if you query for the usertype, you should find it to be a 'Standard' user type.

If you want a different user type, you'll need to apply a different profile.

See also: https://developer.salesforce.com/docs/atlas.en-us.212.0.api.meta/api/sforce_api_objects_user.htm#usertype

The category of user license. Each UserType is associated with one or more UserLicense records. Each UserLicense is associated with one or more profiles

1

Thanks Nick Cook and David Cheng, both of your answers helped! The solution was that I needed to create a Contact object as well and also grab the right profile to be associated with it. The solutions looks something like:

Profile admin = [
    SELECT Id
    FROM Profile
    WHERE Name='System Administrator'
];
Profile regular = [
    SELECT Id
    FROM Profile
    WHERE Name='Client Community Login'
];

Account acct = new Account(Name='Apex Test' + i); 
insert acct;
Contact con = new Contact(LastName='Apex Test' + i, AccountId=acct.Id); 
insert con;
String firstName = (i < 10) ? 'Should Be Inactive' : 'Should Be Active';
Profile p = (i < 10) ? regular : admin;

User u = new User(
    IsActive=true,
    FirstName=firstName,
    LastName=String.valueOf(i),
    Username='test' + i + '@wefowqijefwqofijwfowifjwefoij.com',
    Email='test' + i + '@wefowqijefwqofijwfowifjwefoij.com',
    Alias='test' + i,
    TimeZoneSidKey='America/Denver',
    LocaleSidKey='en_US',
    EmailEncodingKey='UTF-8',
    LanguageLocaleKey='en_US',
    ProfileId=p.Id,
    ContactId=(i < 10) ? con.Id : null,
    UserRoleId=null
);

insert u;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.