You need to sign in to do that
Don't have an account?
Partner Portal - Can't create user that points to Contact in Apex test
I am trying to create a partner portal user in a unit test., but having terrible difficulties. I understand that I must create a Contact and Account first, ensure the Account is a partner account, then create a user that points to the contact. However, I am unable to create a user that points to a contact.
Contact contact1 = new Contact(LastName = 'TestContact'); Database.insert(contact1); Account account1 = new Account(Name = 'TestAccount'); Database.insert(account1); account1.IsPartner = true; Database.update(account1); Profile profile1 = [Select Id from Profile where name = 'System Administrator']; UserRole userRole1 = new UserRole( PortalType = 'Partner', //PortalRole = 'Test', //Name = 'Test Portal Role', PortalAccountId = account1.Id ); Database.insert(userRole1); User user1 = new User( //IsPortalEnabled = true, // 4) Compiler says Field is not writeable. //ContactId = contact1.Id, // 3) Runtime says Only Portal Users can be associated to a Contact. UserType = 'PowerPartner', // 2) Compiler says field not writeable. UserRoleId = userRole1.Id, Username = System.now().millisecond() + 'test12345@test.com', LastName = 'McTesty', Email = 'test12345@test.com', Alias = 'test123', CommunityNickname = 'test12345', TimeZoneSidKey = 'America/Los_Angeles', LocaleSidKey = 'en_US', EmailEncodingKey = 'UTF-8', ProfileId = profile1.Id, LanguageLocaleKey = 'en_US' ); Database.insert(user1); //user1.IsPartner = true; //user1.IsPortalEnabled = true; //Database.update(user1); //user1.ContactId = contact1.Id; // 1) Runtime says role type must match user type. //Database.update(user1);
Can anyone paste some working code for this? Or maybe I'm making an obvious mistake?
Looks like I figured it out. Thanks to James Loghry for the help.
The problem was that I was hacking around with the IsPortalEnabled, IsPartner, UserType, and UserRoleId fields on the User and Account.
The solution is here:
To make a partner portal account, any account is ok. Simply ensure the account's owner has the correct values in the UserRoleId and ProfileId fields.
To make a partner user, any user is ok. Simply ensure the user's profile is a Portal User profile. Then you can specify a value for its ContactId field.
Here's the code that's working for me:
All Answers
Looks like I figured it out. Thanks to James Loghry for the help.
The problem was that I was hacking around with the IsPortalEnabled, IsPartner, UserType, and UserRoleId fields on the User and Account.
The solution is here:
To make a partner portal account, any account is ok. Simply ensure the account's owner has the correct values in the UserRoleId and ProfileId fields.
To make a partner user, any user is ok. Simply ensure the user's profile is a Portal User profile. Then you can specify a value for its ContactId field.
Here's the code that's working for me:
Glad it worked.
Hope it helps!
Regards,
Valnavjo
I faced with the same problem. You can easly fix it by using System.runAs within your test method like this:
Hope that helps!
Regards,
Valnavjo