Thursday, October 19, 2017

Community User Test Data

Always find a hassle to create community user for test data, so just thought putting it out here: 

Normal user creation:

User communityUser = new User(
 ProfileId = [SELECT Id FROM Profile WHERE Name = 'Interconnection Community User Plus User'].Id,
 FirstName = 'first',
 LastName = 'last',
 Email = 'test.test@test.com',
 Username = 'test.' + System.currentTimeMillis() + '@test.com',
 Title = 'Title',
 Alias = 'alias',
 TimeZoneSidKey = 'America/Los_Angeles',
 EmailEncodingKey = 'UTF-8',
 LanguageLocaleKey = 'en_US',
 LocaleSidKey = 'en_US'
);


But we get below errors:

System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Cannot create a portal user without contact: [ContactId]

System.DmlException: Insert failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, portal account owner must have a role: []

System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): Account, original object: User: []


To solve it : use below approach : 

1) Create Portal Owner

   private static User createPortalAccountOwner() {  
     UserRole portalRole = new UserRole(DeveloperName = 'MyCustomRole', Name = 'My Role', PortalType='None' );  
     insert portalRole;  
     System.debug('portalRole is ' + portalRole);  
     Profile sysAdminProfile = [Select Id from Profile where name = 'System Administrator'];  
     User portalAccountOwner = new User(  
         UserRoleId = portalRole.Id,  
         ProfileId = sysAdminProfile.Id,  
         Username = 'portalOwner' + System.currentTimeMillis() + '@test.com',  
         Alias = 'Alias',  
         Email='portal.owner@test.com',  
         EmailEncodingKey='UTF-8',  
         Firstname='Portal',  
         Lastname='Owner',  
         LanguageLocaleKey='en_US',  
         LocaleSidKey='en_US',  
         TimeZoneSidKey = 'America/Los_Angeles'  
     );  
     Database.insert(portalAccountOwner);  
     return portalAccountOwner;  
   }  

2) Create Community User

   private static void createCommunityUser(User portalAccountOwner) {  
     System.runAs ( portalAccountOwner ) {  
       //Create account  
       Account portalAccount = new Account(  
           Name = 'portalAccount',  
           OwnerId = portalAccountOwner.Id  
       );  
       Database.insert(portalAccount);  
       //Create contact  
       Contact portalContact = new Contact(  
           FirstName = 'portalContactFirst',  
           Lastname = 'portalContactLast',  
           AccountId = portalAccount.Id,  
           Email = 'portalContact' + System.currentTimeMillis() + '@test.com'  
       );  
       Database.insert(portalContact);  
       User communityUser = new User(  
           ProfileId = [SELECT Id FROM Profile WHERE Name = 'Interconnection Community User Plus User'].Id,  
           FirstName = 'CommunityUserFirst',  
           LastName = 'CommunityUserLast',  
           Email = 'community.user@test.com',  
           Username = 'community.user.' + System.currentTimeMillis() + '@test.com',  
           Title = 'Title',  
           Alias = 'Alias',  
           TimeZoneSidKey = 'America/Los_Angeles',  
           EmailEncodingKey = 'UTF-8',  
           LanguageLocaleKey = 'en_US',  
           LocaleSidKey = 'en_US',  
           ContactId = portalContact.id  
       );  
       Database.insert(communityUser);  
     }  
   }  

3) We can use below code in testSetup or test method.

 User portalAccountOwner = createPortalAccountOwner();  
 createCommunityUser(portalAccountOwner);  

No comments: