I am having a dilemma here kind of choosing between two errors:
- DML operation on setup object is not permitted after you have updated a non-setup object
- UNKNOWN_EXCEPTION, portal account owner must have a role
Below is my unit test code:
account = new Account();
account.Name = 'TestAccount';
account.Trade_Account_Status__c ='Account Customer';
account.BillingStreet = 'FakeStreet';
account.BillingCity = 'Fake City';
account.BillingState = 'Fake State';
account.BillingPostalCode = 'Fake PostalCode';
account.CanOrderJudgment__c = true;
insert account;
contact = Test_CommonData.CreateContact('Contact', account.Id);
user = Test_CommonData.CreateUser(contact.Id);
And in Test_CommonData class:
public static User CreateUser(Id contactId){
Profile profileObj = [select Id, UserLicense.Name, UserLicenseId from Profile where Name = 'Cortex Standard Customer Monthly - Customer Portal' limit 1];
UserRole usrrole = [Select Id, name From UserRole Where PortalType = 'None' limit 1];
User userObj = new User();
userObj.LastName='Test';
userObj.FirstName='User';
userObj.city='Brisbane';
userObj.Street='Test st';
userObj.State='QLD';
userObj.PostalCode='4000';
userObj.TimeZoneSidKey = 'Australia/Brisbane';
userObj.Username = 'test121@test.com';
userObj.Email = 'test121@test.com';
userObj.CommunityNickname='test121';
userObj.Alias='t100';
userObj.LocaleSidKey = 'en_GB';
userObj.EmailEncodingKey = 'UTF-8';
userObj.LanguageLocaleKey = 'en_US';
userObj.isActive = true;
userObj.ProfileId = profileObj.Id;
userObj.IsPortalSelfRegistered = true;
userObj.contactId = contactId;
userObj.Office_Server_Storage_Location__c = 'C';
userObj.UserRoleId = null;
insert userObj;
return userObj;
}
public static Contact CreateContact(String contactName,Id accountId){
Contact contactObj = new Contact();
contactObj.Contact_Type__c = 'Client';
contactObj.Lastname = contactName;
contactObj.AccountId = accountId;
contactObj.Online_Order_Jurisdiction__c = 'SUP-DJAG;CCJ-DJAG';
contactObj.Audio_Upload_Jurisdiction__c = 'PRIVATE';
contactObj.Show_AudioUpload_Form__c = true;
contactObj.Show_AFP_Form__c = true;
contactObj.Test_AudioUpload__c = true;
insert contactObj;
return contactObj;
}
If I am assigning the userObj's UserRoleId to be null, I am getting portal account owner must have a role error. And if I am assigning the it to be usrrole.Id, I am getting Mixed DML exception.
How should I walk around this issue. I have read all the posts about the previous two issues but doesn't seem to help though.