Creating High Volume Portal Users for unit tests

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='<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a8dbdcc9c6ccc9daccdddbcddae8dccddbdcc7dacf86cbc7c5">[email protected]</a>', 
        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
        LocaleSidKey='en_US', ProfileId = prof.Id, ContactId = c1.Id,
        TimeZoneSidKey='America/Los_Angeles', UserName='<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ef9c9b8e818b8e9d8b9a9c8a9daf9b8a9c9b809d88c18c8082">[email protected]</a>');

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='<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="403334212e242132243533253200342533342f32276e232f2d">[email protected]</a>', 
        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='<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e29196838c8683908697918790a2968791968d9085cc818d8f">[email protected]</a>');

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='<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f4c4b5e515b5e4d5b4a4c5a4d7f4b5a4c4b504d58115c5052">[email protected]</a>', 
    EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
    LocaleSidKey='en_US', ProfileId = prof1.Id, userroleid = r.Id,
    TimeZoneSidKey='America/Los_Angeles', UserName='<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4b7b0a5aab1b7a1b684b0a1b7b0abb6a3eaa7aba9">[email protected]</a>');

    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: []”

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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='<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3848574a4c59544c5d4b4c4d4b5d4a784c5d4b4c574a5f165b5755">[email protected]</a>',
        emailencodingkey='UTF-8', lastname=contactName, languagelocalekey='en_US',
        localesidkey='en_US', profileid = p.Id,
        timezonesidkey='America/Los_Angeles', username='<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="52223d2026333e263721262721372012263721263d20357c313d3f">[email protected]</a>',
         ContactId = c.Id, IsActive=true);
    insert u;

    c.User_Account__c = u.Id;
    update c;

    return u;
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x