+ Start a Discussion
alex_bergalex_berg 

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?

 

Best Answer chosen by Admin (Salesforce Developers) 
alex_bergalex_berg

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:

 

//Create portal account owner
UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
Profile profile1 = [Select Id from Profile where name = 'System Administrator'];
User portalAccountOwner1 = new User(
	UserRoleId = portalRole.Id,
	ProfileId = profile1.Id,
	Username = System.now().millisecond() + 'test2@test.com',
   	Alias = 'batman',
	Email='bruce.wayne@wayneenterprises.com',
	EmailEncodingKey='UTF-8',
	Firstname='Bruce',
	Lastname='Wayne',
	LanguageLocaleKey='en_US',
	LocaleSidKey='en_US',
	TimeZoneSidKey='America/Chicago'
);
Database.insert(portalAccountOwner1);

//Create account
Account portalAccount1 = new Account(
	Name = 'TestAccount',
	OwnerId = portalAccountOwner1.Id
);
Database.insert(portalAccount1);
    	
//Create contact
Contact contact1 = new Contact(
   	FirstName = 'Test',
    	Lastname = 'McTesty',
	AccountId = portalAccount1.Id,
    	Email = System.now().millisecond() + 'test@test.com'
);
Database.insert(contact1);
    	
//Create user
Profile portalProfile = [SELECT Id FROM Profile WHERE Name LIKE '%Portal User%' Limit 1];
User user1 = new User(
	Username = System.now().millisecond() + 'test12345@test.com',
	ContactId = contact1.Id,
	ProfileId = portalProfile.Id,
	Alias = 'test123',
	Email = 'test12345@test.com',
	EmailEncodingKey = 'UTF-8',
	LastName = 'McTesty',
	CommunityNickname = 'test12345',
	TimeZoneSidKey = 'America/Los_Angeles',
	LocaleSidKey = 'en_US',
	LanguageLocaleKey = 'en_US'
);
Database.insert(user1);

 

All Answers

alex_bergalex_berg

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:

 

//Create portal account owner
UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
Profile profile1 = [Select Id from Profile where name = 'System Administrator'];
User portalAccountOwner1 = new User(
	UserRoleId = portalRole.Id,
	ProfileId = profile1.Id,
	Username = System.now().millisecond() + 'test2@test.com',
   	Alias = 'batman',
	Email='bruce.wayne@wayneenterprises.com',
	EmailEncodingKey='UTF-8',
	Firstname='Bruce',
	Lastname='Wayne',
	LanguageLocaleKey='en_US',
	LocaleSidKey='en_US',
	TimeZoneSidKey='America/Chicago'
);
Database.insert(portalAccountOwner1);

//Create account
Account portalAccount1 = new Account(
	Name = 'TestAccount',
	OwnerId = portalAccountOwner1.Id
);
Database.insert(portalAccount1);
    	
//Create contact
Contact contact1 = new Contact(
   	FirstName = 'Test',
    	Lastname = 'McTesty',
	AccountId = portalAccount1.Id,
    	Email = System.now().millisecond() + 'test@test.com'
);
Database.insert(contact1);
    	
//Create user
Profile portalProfile = [SELECT Id FROM Profile WHERE Name LIKE '%Portal User%' Limit 1];
User user1 = new User(
	Username = System.now().millisecond() + 'test12345@test.com',
	ContactId = contact1.Id,
	ProfileId = portalProfile.Id,
	Alias = 'test123',
	Email = 'test12345@test.com',
	EmailEncodingKey = 'UTF-8',
	LastName = 'McTesty',
	CommunityNickname = 'test12345',
	TimeZoneSidKey = 'America/Los_Angeles',
	LocaleSidKey = 'en_US',
	LanguageLocaleKey = 'en_US'
);
Database.insert(user1);

 

This was selected as the best answer
James LoghryJames Loghry

Glad it worked.

 

ValnavjoValnavjo
The best answer worked for me. However be careful with this:
Profile portalProfile = [SELECT Id FROM Profile WHERE Name LIKE '%Portal User%' Limit 1];
As it may not work in your org. Use this instead:
Profile portalProfile = [select Id from Profile where UserType = 'PowerPartner' limit 1];
And this may also not work in your org if its language is not english:
Profile profile1 = [Select Id from Profile where name = 'System Administrator'];
Instead use something like this (and highly recommend using Custom Setting):
Profile adminProfile = [select Id from Profile where Name IN ('System Administrator', 'Administrador del sistema') limit 1];

Hope it helps!


Regards,
Valnavjo

 
Dave Durant 1Dave Durant 1
Does the code in the selected answer still work? It's giving me mixed DML errors in an @isTest, which seems to match the docs for mixing accounts & users with an non-null role. I think it might need some @future rework.
ValnavjoValnavjo
Hello Dave,

I faced with the same problem. You can easly fix it by using System.runAs within your test method like this:
Contact contact = new Contact();
insert contact;

User user = new User();
System.runAs(new User(Id = UserInfo.getUserId())) {
   insert user;
}

Hope that helps!


Regards,

Valnavjo
 

Dave Durant 1Dave Durant 1
Thanks, Valnavjo.. Wow.. I used that on something else just a few days ago - guess my brain is a bit leaky this week.. :\
akshay konijetiakshay konijeti
Thank you this post helped me a lot