[SalesForce] Portal Account Owner Has No Role

I need to test that when I share a record, inactive users are skipped, so I must create a user with IsPortalEnabled = true. I have checked to make sure that I, the logged in user, have a role and am an Admin. I have created a User with the last name of 'NonPortalUser,' System Admin Profile, and a Role. I have used DataLoader to output all of the the users and retrieved the NonPortalUser's OwnerId then hardcoded the assignment of that OwnerId to the Account that is associated with the Community. I have used myself and the NonPortalUser in my System.runAs() calls. I verified that my Guest Users in the Community have a Custom Profile set up.

How can I resolve this error?

Test Class:

@isTest
public class CoopDollarsTriggerHandler_Test2 {
    static List<Id> createAccountsAndContacts(){
        List<Contact> contactsToUse = new List<Contact>();
        List<Account> accountsToUse = new List<Account>();

        PartnerTestDataUtility newPartnerTestData = new PartnerTestDataUtility();
        accountsToUse = newPartnerTestData.createAccounts();
        System.debug('!*!*!*! accountsToUse !*!*!*!' + accountsToUse);

        id distributorId;
        distributorId = accountsToUse[1].Id;
        id distributorId1;
        distributorId1 = accountsToUse[0].Id;
        contactsToUse = newPartnerTestData.createContacts(accountsToUse);
        System.debug('!*!*!*! contactsToUse !*!*!*!' + contactsToUse);

        id contact3Id;
        id contact1Id;
        id contact2Id;
        contact3Id = contactsToUse[2].Id;
        contact1Id = contactsToUse[0].Id;
        contact2Id = contactsToUse[1].Id;
        List<Id> idsToUse = new List<Id>();
        idsToUse.add(distributorId);
        idsToUse.add(contact3Id);
        idsToUse.add(contact1Id);
        idsToUse.add(contact2Id);
        idsToUse.add(distributorId1);
        System.debug('!*!*!*! idsToUse !*!*!*!' + idsToUse);

        return idsToUse;
    }

    static List<Id> createRoleAndUser(List<Id> idsToUse){
        User newPortalUser;
        UserRole role1;
        PartnerTestDataUtility newPartnerTestDataUtility = new PartnerTestDataUtility();
        role1 = newPartnerTestDataUtility.createPartnerUserRole(idsToUse);
        newPortalUser = newPartnerTestDataUtility.createPartnerUser(idsToUse, role1);
        return idsToUse;
    }

    static testMethod void testInsertDollars(){

        User nonPortalUser = [SELECT id, UserRoleId FROM User WHERE LastName = 'NonPortalUser' LIMIT 1];
        System.debug('!*!*!*! nonPortalUser !*!*!*!' + nonPortalUser);
        List<Id> idsToUse = new List<Id>();

        System.runAs(nonPortalUser){
            idsToUse = createAccountsAndContacts();
        }

        System.runAs(nonPortalUser){
            createRoleAndUser(idsToUse);
        }
        test.startTest();
        System.debug('!*!*!*! Starting testInsertDollars !*!*!*!');
        // Code for test here
        test.stopTest();
    }  
}

Utility Class w/Methods for creating Accounts, Contacts, Role, and User:

public without sharing class PartnerTestDataUtility {
    public List<Account> createAccounts(){
        Id accountRecordTypeId;
        List<Account> accountsToReturn = new List<Account>();
        // Get account record type
        List<RecordType> types = [Select Id From RecordType Where SobjectType = 'Account' and Name = 'Distributor'];
        if (types != null && types.size() == 1) {
            accountRecordTypeId = types[0].id;
        }
        // Create Accounts 
        Account dist1 = new Account(
            Name = 'dist1', 
            type = 'Partner',
            RecordTypeId = accountRecordTypeId, 
            BillingStateCode = 'GA', 
            Business_Unit__c = 'South',
        OwnerId = '005e00000047Kxx');            
        insert dist1;
        accountsToReturn.add(dist1);

        Account dist2 = new Account(
            Name = 'dist2', 
            type = 'Partner',
            RecordTypeId = accountRecordTypeId, 
            BillingStateCode = 'GA', 
            Business_Unit__c = 'South',
        OwnerId = '005e00000047Kxx');            
        insert dist2;
    /*            
        User nonPortalUser = [SELECT id FROM User WHERE LastName = 'NonPortalUser' LIMIT 1];
        dist2 = [SELECT Id, Name, IsPartner, OwnerId FROM Account WHERE Id =: dist2.Id];
        System.debug('!*!*!*! dist2 - Before Update !*!*!*!' + dist2);
        dist2.OwnerId = '005e00000047Kxx';
        update dist2;
        System.debug('!*!*!*! dist2 - After Update !*!*!*!' + dist2);
    */            
        dist2 = [SELECT Id, Name, IsPartner, OwnerId FROM Account WHERE Id =: dist2.Id];
        dist2.IsPartner = true;
        update dist2;
        System.debug('!*!*!*! dist2 - After Update !*!*!*!' + dist2);
        accountsToReturn.add(dist2);

    for(Account acts : accountsToReturn){
        if(acts.Owner.UserRoleId == null){
            System.debug('!*!*!*! acts.Owner !*!*!*!' + acts.Owner);
            System.debug('!*!*!*! acts.Owner.UserRoleId !*!*!*!' + acts.Owner.UserRoleId);
            acts.OwnerId = '005e00000047Kxx';
            update accountsToReturn;
        }
    }
    System.debug('!*!*!*! accountsToReturn !*!*!*!' + accountsToReturn);

        return accountsToReturn;
    } 

    public List<Contact> createContacts(List<Account> accountsToUse){
        List<Contact> contactsToUse = new List<Contact>();
        Contact contact1 = new Contact(
            LastName = 'contact1',
            Business_Unit__c = 'South',
            Account = accountsToUse[0],
            email = 'test@hvac.mea.com');
        insert contact1;
        contactsToUse.add(contact1);

         Contact contact2 = new Contact(
            LastName = 'contact2',
            Business_Unit__c = 'South',
            Account = accountsToUse[1],
            email = 'test@hvac.mea.com');
        insert contact2;
        contactsToUse.add(contact2);

        Contact contact3 = new Contact(
            LastName = 'contact3',
            Business_Unit__c = 'South',
            Account = accountsToUse[1],
            email = 'test@hvac.mea.com');
        insert contact3;
        contactsToUse.add(contact3);

        return contactsToUse;
    }

    Public UserRole createPartnerUserRole(List<Id> idsToUse){
        UserRole role2 = new UserRole();
            role2.portalType = 'Partner'; // use 'CustomerPortal' for customer portal roles
            role2.PortalAccountId = idsToUse[0];
            role2.CaseAccessForAccountOwner = 'Read'; //Modify as needed
            role2.OpportunityAccessForAccountOwner = 'Read'; //Modify as needed
        insert role2;
        System.debug('!*!*!*! role2 !*!*!*!' + role2);
        return role2;
    }

    Public User createPartnerUser(List<Id> idsToUse, UserRole role1){
        Profile profileId2 = [SELECT Id FROM Profile WHERE Name = 'Distributor Community User - Commercial' LIMIT 1];
        // Profile profileId2 = [SELECT Id FROM Profile WHERE Name = 'System Administrator' LIMIT 1];
        // role1 = [SELECT Id FROM UserRole WHERE PortalType = 'Partner' LIMIT 1];
        Contact contact3 = [SELECT Id, FirstName, LastName FROM Contact WHERE Contact.Id =: idsToUse[1]];
        System.debug('!*!*!*! contact3 !*!*!*!' + contact3);
        User u2 = new User(LastName = 'TestLastName',
                           FirstName='TestFirstName',
                           Alias = 'tln2',
                           Email = 'test@hvac.mea.com',
                           Username = 'testUser2@hvac.mea.com',
                           IsActive = True,
                           ProfileId = profileId2.id,
                           communitynickname = 'newCommunityUser',
                           // UserRoleId = role1.Id,
                           PortalRole = 'Worker',
                           TimeZoneSidKey = 'GMT',
                           LanguageLocaleKey = 'en_US',
                           EmailEncodingKey = 'UTF-8',
                           LocaleSidKey = 'en_US',
                           ContactId = contact3.Id);
        System.debug('!*!*!*! u2 !*!*!*!' + u2);
        insert u2;

        u2 = [SELECT Id, IsPortalEnabled, IsActive FROM User WHERE id =: u2.Id];
        System.debug('!*!*!*! u2 - After Insert !*!*!*!' + u2);
        u2.IsPortalEnabled = true;
        u2.IsActive = false;
        update u2;
        System.debug('!*!*!*! u2 - After Update !*!*!*!' + u2);
        return u2;
    }
}

Best Answer

I believe that it's not the system.runas() that is the problem here.

It's the Accounts that you created, and the Owner of the Accounts(whoever is running the unit test at the time), and those Owners Roles.

So I'd be debugging something more like: ListOfAccounts[0].Owner.UserRoleId < is this null? (Don't forget to re-soql the accounts after insert, otherwise this will always return null)

To specify the Account Owner, you can do it up creation, or use the system.runas command for the "insert dist1" line. The Owner also needs to be Active.

You can also debug: UserInfo.getUserRoleId() < this should be the Accounts Owners RoleId

Let me know if this points you in the direction you need!