You need to sign in to do that
Don't have an account?

test class error System.SObjectException: Field is not writeable: User.ProfileId
/** * An apex page controller that supports self registration of users in communities that allow self registration */ public class CommunitiesSelfRegController { public String firstName {get; set;} public String lastName {get; set;} public String email {get; set;} public String password {get; set {password = value == null ? value : value.trim(); } } public String confirmPassword {get; set { confirmPassword = value == null ? value : value.trim(); } } public String communityNickname {get; set { communityNickname = value == null ? value : value.trim(); } } public CommunitiesSelfRegController() {} private boolean isValidPassword() { return password == confirmPassword; } public PageReference registerUser() { // it's okay if password is null - we'll send the user a random password in that case if (!isValidPassword()) { ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, Label.site.passwords_dont_match); ApexPages.addMessage(msg); return null; } String profileId = null; // To be filled in by customer. String roleEnum = null; // To be filled in by customer. String accountId = ''; // To be filled in by customer. String userName = email; User u = new User(); u.Username = userName; u.Email = email; u.FirstName = firstName; u.LastName = lastName; u.CommunityNickname = communityNickname; u.ProfileId = profileId; String userId; try { userId = Site.createExternalUser(u, accountId, password); } catch(Site.ExternalUserCreateException ex) { List<String> errors = ex.getDisplayMessages(); for (String error : errors) { ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, error)); } // This message is used for debugging. Do not display this in the UI to the end user. // It has the information around why the user creation failed. System.debug(ex.getMessage()); } if (userId != null) { if (password != null && password.length() > 1) { return Site.login(userName, password, ApexPages.currentPage().getParameters().get('startURL')); } else { PageReference page = System.Page.CommunitiesSelfRegConfirm; page.setRedirect(true); return page; } } return null; } } /*Test Class*/ @IsTest public with sharing class CommunitiesSelfRegControllerTest { @IsTest(SeeAllData=true) public static void testCommunitiesSelfRegController() { CommunitiesSelfRegController controller = new CommunitiesSelfRegController(); controller.firstName = 'FirstName'; controller.lastName = 'LastName'; controller.email = 'test@force.com'; controller.communityNickname = 'test'; // registerUser will always return null when the page isn't accessed as a guest user System.assert(controller.registerUser() == null); controller.password = 'abcd1234'; controller.confirmPassword = 'abcd123'; System.assert(controller.registerUser() == null); } static testMethod void TestProfileType2(){ test.startTest(); CommunitiesSelfRegController controller = new CommunitiesSelfRegController(); Profile p = [SELECT Id FROM Profile WHERE Name='Standard User']; User u1 = new User(username='testsfsdfsd@test.com', IsActive=TRUE, FirstName='test', Alias = 'standt1',Country='United Kingdom',Email='demo1@randomdemodomain.com', EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', LocaleSidKey='en_US',ProfileId = p.Id,TimeZoneSidKey='America/Los_Angeles'); insert u1; // now insert your test data System.runAs(u1){ controller.registerUser(); // you test for your controller test.startTest(); } }
It sounds like the User Profile being used to run the system as does not have the right to access the field