You need to sign in to do that
Don't have an account?
rajjjj
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.
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.
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.
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!!!!
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.
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.... :)