Я создал пользователя сообщества портала для тестирования.Я запускаю тест как пользователь портала, но идентификатор учетной записи, который связан с контактом, связанным с пользователем портала, является нулевым
Я использую System.runAs (portalUser) {...}
private static User createPortalUser() {
User portalUser;
// Create an Admin User linked to UserRole and Administrator Profile
UserRole portalRole = [Select Id From UserRole Where PortalType = 'None' Limit 1];
Profile adminProfile = [Select Id from Profile where name = 'System Administrator'];
User adminUser = new User();
adminUser.UserRoleId = portalRole.Id;
adminUser.ProfileId = adminProfile.Id;
adminUser.Username = System.now().millisecond() + 'admin@test.com';
adminUser.Alias = 'admAlias';
adminUser.Email='admin@test.com';
adminUser.EmailEncodingKey='UTF-8';
adminUser.Firstname='Admin';
adminUser.Lastname='Test';
adminUser.LanguageLocaleKey='en_US';
adminUser.LocaleSidKey='en_US';
adminUser.TimeZoneSidKey='America/Los_Angeles';
insert(adminUser);
/**
* Run as Admin User in order to create a new Portal User
* The Portal User is linked to a Contact that is linked to Account
**/
System.runAs(adminUser) {
//Create Account
Account portalAccount = new Account();
portalAccount.Name = 'TestAccount';
portalAccount.Status__c = 'Active';
insert(portalAccount);
//Create Contact
Contact contTest = new Contact();
contTest.Lastname = 'Contact Test';
contTest.AccountId = portalAccount.Id;
insert(contTest);
/**
* Create User
* Retrieve Portal Profile to link to the Portal User
* Link the Contact to the Portal User
**/
Profile portalProfile = [SELECT Id FROM Profile WHERE Name = 'Israel Postal Community Login User'];
portalUser = new User();
portalUser.Username = System.now().millisecond() + 'user@test.com';
portalUser.ContactId = contTest.Id;
portalUser.ProfileId = portalProfile.Id;
portalUser.Alias = 'userTest';
portalUser.Email = 'user@test.com';
portalUser.EmailEncodingKey = 'UTF-8';
portalUser.LastName = 'UserTest';
portalUser.CommunityNickname = 'NicknameTest';
portalUser.TimeZoneSidKey = 'America/Los_Angeles';
portalUser.LocaleSidKey = 'en_US';
portalUser.LanguageLocaleKey = 'en_US';
insert(portalUser);
if (portalUser.ContactId != null) {
createServiceContractLinkedToPortalUser(portalAccount.Id);
//createAccountContactRelationObject(portalAccount);
} else {
System.debug('Connection to Portal User has failed');
}
}
return portalUser;
}
В моих методах тестирования мне нужно вставить в качестве параметра идентификатор учетной записи текущего работающего пользователя