I'm doing it as follows: `ProfileId = [SELECT Id FROM Profile WHERE Name = 'Comercial' LIMIT 1].Id`, but it is returning the error: `System.DmlException: Update failed. First exception on row 7 with id 005Kj00000AvzLaIAJ; first error: INSUFFICIENT_ACCESS_OR_READONLY, Cannot insert a user with this profile: []`.
(That's all my user fields) User testUser = new User(
LastName = 'Test User',
Email = 'testuser@test.com',
Username = 'testuser@test.com' + System.currentTimeMillis(),
Alias = 'testu',
TimeZoneSidKey = 'America/Sao_Paulo',
LocaleSidKey = 'pt_BR',
EmailEncodingKey = 'UTF-8',
ProfileId = [SELECT Id FROM Profile WHERE Name != 'Administrador do Sistema' limit 1].Id,
LanguageLocaleKey = 'pt_BR',
UserRoleId = role.Id,
Aprovacao_Disponivel_Novo__c = 10,
Aprova_o_Disponivel_Seminovo__c = 20
);
insert testUser;
How can I fix it?
The query returns more than one profile, and I would suspect that is the issue. You need to run a query with just one result. Currently your result is basically all profiles except Sys Admin.