2

I am building some test data in my unit test.

Roughly I have something like this:

@IsTest
static void getUserTest() {
    Account acct = new Account();
    acct.Name = 'Test Account';
    insert acct;

    Contact contact = new Contact();
    contact.LastName = 'Test';
    contact.FirstName = 'Test';
    contact.accountId = acct.Id;
    insert contact;

    User adminUser = new User();
    adminUser.FirstName = 'Test';
    adminUser.LastName = 'Test';
    adminUser.Alias = 'Test';
    adminUser.Email = '[email protected]';
    adminUser.UserName = '[email protected]';
    adminUser.EmailEncodingKey = 'UTF-8';
    adminUser.LanguageLocaleKey = 'en_US';
    adminUser.LocaleSidKey = 'en_US';
    adminUser.TimezoneSidKey = 'America/Los_Angeles';
    adminUser.ProfileId = [
                SELECT Id
                FROM Profile
                WHERE Name = 'System Administrator'
        ].Id;
    insert adminUser;

    UserRole userRole = [SELECT Id FROM UserRole WHERE PortalType = 'CustomerPortal' LIMIT 1];

    User testUser;
    System.runAs(adminUser) {
        testUser = new User();
        testUser.FirstName = 'Test2';
        testUser.LastName = 'Test2';
        testUser.Alias = 'Test2';
        testUser.Email = '[email protected]';
        testUser.UserName = '[email protected]';
        testUser.EmailEncodingKey = 'UTF-8';
        testUser.LanguageLocaleKey = 'en_US';
        testUser.LocaleSidKey = 'en_US';
        testUser.TimezoneSidKey = 'America/Los_Angeles';
        testUser.ContactId = contact.Id;
        testUser.ProfileId = [
                        SELECT Id
                        FROM Profile
                        WHERE Name = 'Customer Self Service Community Profile'
                        ].Id;
        testUser.UserRoleId = userRole.Id;
        insert testUser;
    }
}

The insert testUser line gives me the following error:

System.DmlException: Insert failed. First exception on row 0; 
first error: FIELD_INTEGRITY_EXCEPTION, Invalid role assignment.: []

The above code is working correctly in one sandbox but fails in another. Both sandboxes are super out of sync(out of my control). Any ideas what I am doing wrong?

8
  • 1
    Customer Selft Service Community Profile ?? Typo?
    – cropredy
    Commented Jun 12, 2019 at 20:50
  • @cropredy Thanks for pointing that out but that's not the problem(unfortunately). I guess I put the t in the code snippet as I was copying it out of the class in question. Sorry about that.
    – Arthlete
    Commented Jun 12, 2019 at 20:57
  • Are you not creating an Account here?
    – Jayant Das
    Commented Jun 13, 2019 at 1:09
  • @JayantDas Sorry missed adding this to my code snippet. I have an account and still get the same error.
    – Arthlete
    Commented Jun 13, 2019 at 3:07
  • 1
    So here’s how it works. That one is where you are creating the portal user (you don’t need a role unless required). What you will need to ensure is that the OwnerId of the Account here has a role assigned. In this case it will be most likely the adminUser where you assign a role to that user while creating and then use acct.OwnerId=adminUser.Id. Finally create the portal user.
    – Jayant Das
    Commented Jun 13, 2019 at 3:24

3 Answers 3

8

You a have to assign the role to adminUser as well. Also, the sequence of steps also needs to be changed. Sequence should be as follows

  • Create adminUser with the role assigned to this user (This role should be other than the customer portal role).
  • Create Account and Contact while running in adminUser context i.e. enclose your Account and Contact creation code within system.runAs(adminUser). This is because, the Owner of the Account should have a role.
  • Create Community User as adminUser i.e. after creating Account and Contact, your code for Community User creation should follow which should be within system.runAs(adminUser). You can assign PortalRole for this user or can leave it blank, when this field is left blank and a Contact is assigned, the user is automatically assigned a User Role.

Excerpt from Salesforce User documentation

PortalRole

The role of the user in the Customer Portal (either Executive, Manager, User, or PersonAcount). Prior to API version 16.0, you could set this field to null and the system automatically included a portal role. In API version 16.0 and above, when you set this field to null, a portal role is not automatically created. When this field is null and a ContactId is provided, the user is assigned to the User role.

6
  • Thanks, I will give it a try.
    – Arthlete
    Commented Jun 13, 2019 at 3:30
  • I tried what you suggested but get this error FIELD_INTEGRITY_EXCEPTION, role type must match user type. I guess it's because the user is a system admin but I am trying to assign a CustomerPortal role to it.
    – Arthlete
    Commented Jun 13, 2019 at 3:42
  • 1
    Yes, you answered your question. Assign a role other than customer portal role to system admin and then give a try. Commented Jun 13, 2019 at 4:24
  • I still get the same error Invalid role assignment.: []. Could it have something to do with communities being inactive? Can the unit test code see the communities?
    – Arthlete
    Commented Jun 13, 2019 at 4:43
  • 1
    Assign role only to adminUser and don't assign any role to community user Commented Jun 13, 2019 at 4:47
2

This worked for me

    RecordType rt = [Select Id From RecordType Where DeveloperName = 'Staff' limit 1];
    RecordType accountRT = [Select Id From RecordType Where DeveloperName = 'organization' limit 1];
    UserRole r1 = [SELECT Id FROM UserRole WHERE DeveloperName = 'YourUserRole' limit 1];
    Profile portalProfile = [Select Id from Profile where name = 'Your Operator Portal User'];
    Contact portalContact;
    
    User thisUser = new User(Id=UserInfo.getUserId());
    thisUser.UserRoleId = r1.Id;
    update thisUser;
    System.runAs(thisUser) {
        Account portalAccount = new Account(name = 'portalAccount',RecordTypeId=accountRT.Id);//create a portal account first
        insert portalAccount;
        portalContact = new contact(LastName = 'portalContact', AccountId = portalAccount.Id,RecordTypeId = rt.Id); //create a portal contact
        insert portalContact;
    }
    User u1 = new User( email='[email protected]',
                        profileid = portalProfile.Id, 
                        UserName='[email protected]', 
                        Alias = 'a',
                        TimeZoneSidKey='Australia/Sydney',
                        EmailEncodingKey='ISO-8859-1',
                        LocaleSidKey='en_US', 
                        LanguageLocaleKey='en_US',
                        ContactId = portalContact.Id,
                        FirstName = 'Test FirstName',
                        LastName = 'LastName'
                      );
    
    insert u1;

Hope this helps! If this helps kindly mark it as solved so that it may help others in future.

Thanks, Parmanand

0

This worked for me

UserRole ur = [Select Id, DeveloperName From UserRole Where PortalType <> 'Partner' Limit 1];
User thisUser = new User(Id=UserInfo.getUserId());
    thisUser.UserRoleId = ur.Id;
    update thisUser;
    System.runAs(thisUser) {
        Account portalAccount = new Account(name = 'portalAccount',RecordTypeId=accountRT.Id);//create a portal account first
        insert portalAccount;
        portalContact = new contact(LastName = 'portalContact', AccountId = portalAccount.Id,RecordTypeId = rt.Id); //create a portal contact
        insert portalContact;
    }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .