Im trying to create a record of an Object (DelegatedAccount) in a Test class inside the test Setup method. Its an entity that has 2 Lookups - To Account and User Object. while creating the User record I have created a new UserRole record and linked it to the User since the mandate is that the User linked to the DelegatedAccount must have a role. Im getting the following error when I am running a DML on DelegatedAccount and I can't understand where I am going wrong. I am using System.runAs to prevent mixed DML error as Im doing DML on both Setup and Non SetUp Objects.

public  class MCX_AccountHelperTest {

    @testSetup static void setup() {
        List<Account> accList = new List<Account>();
        List<User> userList = new List<User>();
        List<DelegatedAccount> delAccList = new List<DelegatedAccount>();
          User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
UserRole userRoleVal = new UserRole(DeveloperName = 'MyCustomRole', Name = 'My Role');
insert userRoleVal;
 Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
  User u = new User(Alias = 'standt', Email='[email protected]', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id,  UserRoleId = userRoleVal.Id,
            TimeZoneSidKey='America/Los_Angeles', UserName='[email protected]');


        insert userList;
        for(Integer i=0; i<20; i++){
            accList.add(new Account(Name ='Test Account'+i, MC_Status__c='Active', MC_Account_Number__c = '123445'+i,MC_Division__c ='Automobile'));
        insert accList;
        for(Integer i=0;i<20;i++){
            DelegatedAccount delAcc = new DelegatedAccount();
            delAcc.Name ='Test EMA'+i;
            delAcc.TargetId = accList[i].Id;
            delAcc.ManagedById = userList[0].id

      insert delAccList;
    public static void getDelegatedAccountsTest(){
      List<DelegatedAccount> delList = MCX_AccountHelper.getDelegatedAccounts();
        System.assertEquals(20, delList.size());

Im getting System.DmlException: Insert failed. First exception on row 0; first error: INVALID_FIELD_FOR_INSERT_UPDATE, Enter a role-based external user.: [ManagedById] I cant understand is it not able to detect the User that I have declared above?

Update: I applied a debug here to see if Im getting the User details here-

 System.debug('User is'+ userList);
  delAcc.ManagedById = userList[0].Id;

This is what I got: DEBUG|User is(User:{Alias=standt, [email protected], EmailEncodingKey=UTF-8, LastName=Testing, LanguageLocaleKey=en_US, LocaleSidKey=en_US, ProfileId=00e4P000001Kj92QAC, UserRoleId=00ED70000029anzMAA, TimeZoneSidKey=America/Los_Angeles, [email protected], Id=005D700000EtpV1IAJ})

so I believe the User Data is coming properly. Also the DelegatedObject has only ReadAccess but Apex runs in System mode so I believe that shouldnt be an issue because from Anonymous Window I tried with an existing User and it worked.

1 Answer 1


The issue here is the User that you're trying to assign to the ManagedBy value. More specifically, the issue is the type of user you're trying to assign.

As specified in the special access rules for the DelegatedAccount object the user needs to have a Partner or Community Plus license. Looking at your code you're using an internal user, so updating your code to create a new Community/Partner user will fix this error.

  • So a Role is not needed? because I did exactly that. Just fetched the profile Id of a Community Plus profile, created an account, created a contact and associated that contact to that account. Then when I created the User I linked to it the contact and the profile Id. Did not associate to it any role but my Test class worked.
    – Amax1
    Jun 20, 2023 at 4:38
  • The error message you received is somewhat ambiguous, which is frustrating. Partner and Community Plus Users support roles, which is what is required here, rather than requiring a role to be set up from my understanding. Jun 20, 2023 at 4:59

You must log in to answer this question.

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