colorful-marbles-candies-orbs-beads-circular-mandala-loop-video

Create A Customer Portal User In Test Class

Sometimes we need to create customer portal user in test class to run the functionality of customer user with  System.runAs() method.

Here I’ll provide how we create customer portal user in test class with some basic points.

First we need to understand which profile is assigned to the portal user. When we enabled customer portal for any salesforce org, some standard profiles are already there like ‘Authenticated Website’, ‘High Volume Customer Portal’ etc. and you can clone that profile to extend the privileges.

To assign profile to customer portal  user in apex we need to query the profile where userType falls in ‘CSPLiteUser’, ‘PowerPartner’, ‘PowerCustomerSuccess’,  ‘CustomerSuccess’.

Second we need to understand the role i.e which role is assigned to the portal user.The portal roles are unique for each account and include the account’s name—or example, “Account A Customer User.” In your organization’s overall role hierarchy, this account-specific hierarchy is directly below the account owner.

To assign role to customer portal user in apex we need to query the UserRole where PortalType equals to  ‘CustomerPortal’ .

/******** Create Customer portal user without having any role in test class -START *********/           
Set customerUserTypes = new Set {'CSPLiteUser', 'PowerPartner', 'PowerCustomerSuccess',   'CustomerSuccess'};
Account acc = new Account (
Name = 'newAcc1'
);  
insert acc;
Contact con = new Contact (
AccountId = acc.id,
LastName = 'portalTestUser'
);
insert con;
Profile p = [select Id,name from Profile where UserType in :customerUserTypes limit 1];
 
User newUser = new User(
profileId = p.id,
username = '[email protected]',
email = '[email protected]',
emailencodingkey = 'UTF-8',
localesidkey = 'en_US',
languagelocalekey = 'en_US',
timezonesidkey = 'America/Los_Angeles',
alias='nuser',
lastname='lastname',
contactId = con.id
);
insert newUser;
/******** Create Customer portal user without having any role in test class - END *********/

But If you need to add Role in the newly created portal user then query the UserRole and assigned that role to the user.But there is a problem when we do that.

It gives an error “DML operation on setup object is not permitted after you have updated a non-setup object and vice versa original object – Account ,User”.To overcome this error you need to put all parts of the test that access normal objects need to be wrapped in a System.runAs that explicitly uses the current user ,as below :

/******** Create Customer portal user with a role in test class -START *********/ 
Set customerUserTypes = new Set {'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 = '[email protected]',
email = '[email protected]',
emailencodingkey = 'UTF-8',
localesidkey = 'en_US',
languagelocalekey = 'en_US',
timezonesidkey = 'America/Los_Angeles',
alias='nuser',
lastname='lastname',
contactId = con.id
);
insert newUser;  
}
    /******** Create Customer portal user with a role in test class -END *********/
711 400 Burnignorance | Where Minds Meet And Sparks Fly!