I've been trying to make a unit test recently that requires of UserRole
s with a certain PortalRole
and PortalAccountId
, as well as a Group
related to the previous role.
I tried doing it like this:
@TestSetup
static void dataSetup() {
Account account = TestFactory.createAccount();
insert account;
User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
System.runAs(thisUser){
UserRole role = new UserRole(Name = 'My Nice Role', PortalRole = 'Executive', PortalAccountId = account.Id);
insert role;
User user = TestFactory.createUser();
user.UserRoleId = role.Id;
insert user;
Group grp = new Group(Type = 'RoleAndSubordinates', RelatedId = role.Id);
insert grp;
}
}
But the problem is the fields aren't writeable. After searching the internet for a while I found out that those fields are written by some Salesforce internal processes, but I can't seem to figure out how to achieve it. What or how do I need to insert or these records to make them have these fields?
Thanks