+ Start a Discussion
rajjjjrajjjj 

Creating a customer portal user in test class

I was trying to create a customer portal user in test class. But I was getting mixed dml exception, invalid role assignment,...

Please post the working code if someone already created.

 

Thanks,

Raj.

Puja_mfsiPuja_mfsi

Hi,

You can use the below code :

 

@IsTest(SeeAllData=true) 

public class CustomerPortalTest {
     public static void unitTest() {
        
        Set<String> customerUserTypes = new Set<String> {'CSPLiteUser', 'PowerPartner', 'PowerCustomerSuccess',   'CustomerSuccess'};
        Account acc = new Account (
            Name = 'newAcc1'
        );   
        insert acc;
        Contact con = new Contact (
            AccountId = acc.id,
            LastName = 'portalTestUser'
        );
        insert con;
        User thisUser = [ select Id from User where Id = :UserInfo.getUserId() ];
        
        System.runAs ( thisUser ) {
            UserRole ur = [Select PortalType, PortalAccountId From UserRole where PortalType =:'CustomerPortal' limit 1];
            Profile p = [select Id,name from Profile where UserType in :customerUserTypes limit 1];
            
            User newUser = new User(
                UserRoleId = ur.Id,
                profileId = p.id, 
                username = 'newUser@yahoo.com', 
                email = 'pb@ff.com', 
                emailencodingkey = 'UTF-8', 
                localesidkey = 'en_US', 
                languagelocalekey = 'en_US', 
                timezonesidkey = 'America/Los_Angeles', 
                alias='nuser', 
                lastname='lastname', 
                contactId = con.id
            );
            insert newUser;   
        }
    }
}

 

 

First you need to add all the userType related to customer portal use in a set to query their profile.Insert one account and one contact related to the same account.Query on profile and UserRole ( check PortalType =:'CustomerPortal' ) ,after that insert user with appropriate fields.

NOTE : When you insert a user with UserRole the error "DML operation on setup object is not permitted after you have updated a non-setup object" is occured.to overcome this error you need to add the whole code inside 
System.runAs ( LoginUser ) { 

}

And need to add "SeeAllData=true" in test class.

 


If this post is helpful please give kudos ( click on star at left ),Please let me know if you have any issues regarding the same.

 

rajjjjrajjjj

Hi Puja,

 

I have resolved my issue of Inserting Portal User this way.

I need to setup ownerId and that owner should be system admin and he should have some role.

 

Then you will not get invalid role assignment.

 

I haven't tried your code yet.

 

        User thisUser = [select Id from User where Id = :UserInfo.getUserId()];
        ID ProfileID = [ Select id,UserType from Profile where name = 'Support Customer Portal User Standard' and UserType='PowerCustomerSuccess'].id;
        
        Account A1 = new Account(Name = 'Test Account', OwnerId=thisUser.Id );
        insert A1;
        
        Contact C1 = new Contact( AccountID = A1.id, FirstName = 'Test', LastName = 'User', 
        email = 'test-user@fakeemail.com' );
        insert C1; 
        
        User u1 = new User( email='test-user@fakeemail.com', contactid = c1.id, profileid = profileID, 
                  UserName='test-user@fakeemail.com', alias='tuser1', CommunityNickName='tuser1', 
                  TimeZoneSidKey='America/New_York', LocaleSidKey='en_US', EmailEncodingKey='ISO-8859-1', 
                  LanguageLocaleKey='en_US', FirstName = 'Test', LastName = 'User');
        insert u1;
        User u = [select Id,usertype from User where Id =: u1.Id];
        
        AccountShare AccShare = new AccountShare(AccountId = A1.Id, 
                                AccountAccessLevel = 'Edit', OpportunityAccessLevel = 'Read', UserOrGroupId = u.Id);
        Insert AccShare;
        
        System.RunAs(u)
        {
            con.getPdf();
            system.debug('u.UserType inside------->'+u.UserType);           
        }

 

But I was getting one more issue now when inserting AccountShare. After inserting AccountShare, Its giving me System.QueryException: sObject type 'AccountShare' is not supported.

In my controller I have a query on AccountShare and test is getting failed.

 

Please suggest something!!!!

 

 

Puja_mfsiPuja_mfsi

Hi,

I am not sure what is the issue,But use "without sharing" keyword  in your class.

"System.QueryException: sObject type 'AccountShare' is not supported" exception is occured when you don't have access of the particular object.

 

Please let me know if  the issue is persist.

 

rajjjjrajjjj
By deafult it is with sharing...I can try with "without sharing" to give access to AccountShare, But my class needs to run with "with sharing".

So what I was doing is skip that 3 or 4 lines with a Boolean Variable and the exception was gone. I covered 89%...so I am not gonna bother about it right now.... :)