8

I am attempting to test some code written for an inline visualforce page. This code operates under the assumption that the user viewing it has a contact associated to it. I am attempting to create the User and run my tests as that portal user. I am running into issues inserting the User object. The first error I receive is that the "portal account owner must have a role" at which point I add a role to the creation of the user object and receive the error "High Volume Portal Users cannot have a user role". Maybe I have a misconception or misunderstanding that is causing this problem. Here is the code that I have written to create the user for the first test:

Profile prof = [SELECT Id FROM Profile WHERE Name='CustomProfile With High Volume Web User']; 
    User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
        LocaleSidKey='en_US', ProfileId = prof.Id, ContactId = c1.Id,
        TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');

and this code when I get the second error

     UserRole r = new UserRole(name = 'TEST ROLE');
     insert r;

     Profile prof = [SELECT Id FROM Profile WHERE Name='CustomProfile With High Volume Web User']; 
        User u = new User(Alias = 'standt', Email='standarduser@testorg.com', 
        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
        LocaleSidKey='en_US', ProfileId = prof.Id, ContactId = c1.Id, userroleid = r.Id,
        TimeZoneSidKey='America/Los_Angeles', UserName='standarduser@testorg.com');

Thanks for your help.

EDIT***

I tried implementing what @JimRae recommended but am getting a DML error when attempting to insert them the way he stated.

UserRole r = new UserRole(name = 'TEST ROLE');
    insert r;

    Profile prof1 = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
    User u1 = new User(Alias = 'standt', Email='standarduser@testorg.com', 
    EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
    LocaleSidKey='en_US', ProfileId = prof1.Id, userroleid = r.Id,
    TimeZoneSidKey='America/Los_Angeles', UserName='stanuser@testorg.com');

    insert u1;

    Account a = new Account();
    a.OwnerId = u1.Id;
    insert a;

The error reads:

"13:26:10:192 EXCEPTION_THROWN [21]|System.DmlException: Insert failed. First exception on row 0; first error: MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): Account, original object: User: []"

10

It is the Owner of the Account the Portal User Contact record that needs to have the role. The HVP User will inherit the role hierarchy from the Accounts Owners profile. First, create the user that will own the account, and make sure they have a Role. Then create the Account, owned by this user, then, the contact related to the account. Last, create the HPU and include the contactid of the contact you created. Then, use it for your test.

  • Yeah but how do I create these. When attempting to do this in my test classes I get a Mixed DML Error. I updated my original question to show the error I got and code I tired. – Chris Nov 14 '13 at 18:32
  • Basically, you can use either the "Start Test" or "Stop Test" method or the "System.runAs" method. See this documentation for full details: salesforce.com/us/developer/docs/apexcode/Content/… – JimRae Nov 14 '13 at 20:17
-1

The user that is trying to create the portal user needs a role. In this case it is a script that is trying to create that user and that script runs with your permissions, so YOUR OWN PROFILE needs to have a role.

This is the utility method I use to create a test portal user:

public static User portalUserWithProfile(String profileName, String accountName, String contactName) {

    Contact c = NewContact(NewAccount(accountName), contactName);

    Profile p = [select id from profile where name = :profileName];

    User u = new User(Alias='ptest', email='portaltestuser@testorg.com',
        emailencodingkey='UTF-8', lastname=contactName, languagelocalekey='en_US',
        localesidkey='en_US', profileid = p.Id,
        timezonesidkey='America/Los_Angeles', username='portaltestuser@testorg.com',
         ContactId = c.Id, IsActive=true);
    insert u;

    c.User_Account__c = u.Id;
    update c;

    return u;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.