function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Krishna Sahu 1Krishna Sahu 1 

Portal user must have an role i am getting this error while exceuting mt test class please help me on this

 @isTest
    private static void VerifyPopulatePhysicianOnCareTeam(){
        User adminUser = [Select Id, UserRoleId From User Where Profile.Name='System Administrator' AND isActive = true Limit 1];
        System.runAs(adminUser){
            Test.startTest();
            Account businessAccount = TestUtility.createAccounts(1, 'Test', false)[0];
            businessAccount.Type = 'Customer';
            insert businessAccount;
            List<Account> listPersonAccount = TestUtility.createPatientAccounts(1, 'Patient Account', true);
            Id RecordTypeIdValue =SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Provider').getRecordTypeId();
            Contact physicianContact = new Contact(LastName = 'physician Contact', AccountId = businessAccount.Id , RecordTypeId = RecordTypeIdValue);
            insert physicianContact;
            User user = new User();
            user.ProfileID = [Select Id From Profile Where Profile.Name='Ext. Physician'].Id;
            user.EmailEncodingKey = 'ISO-8859-1';
            user.LanguageLocaleKey = 'en_US';
            user.TimeZoneSidKey = 'America/New_York';
            user.LocaleSidKey = 'en_US';
            user.FirstName = 'first';
            user.LastName = 'last';
            user.Username = 'test@edomain.com';
            user.CommunityNickname = 'testUser123';
            user.Alias = 't1';
            user.Email = 'no@email.com';
            user.UserRoleId = 'US - Finance';
            user.IsActive = true;
            user.ContactId = physicianContact.Id;
            insert user; 
            Order orderObj = new Order();
            orderObj.EffectiveDate = system.today();
            orderObj.EndDate = system.today() + 4;
            orderObj.AccountId = businessAccount.Id;
            orderObj.patient_account__c=listPersonAccount[0].Id;
            orderObj.Implanting_physician__c=user.ContactId;
            orderObj.Type='NROT';
            insert orderObj;
            Test.stopTest();
            List<CareTeam__c> objCareTeam = [SELECT Id, Name, Member__r.ContactId, Patient__c 
                                             FROM CareTeam__c 
                                             WHERE Patient__c =: orderObj.patient_account__c LIMIT 1];
            System.assertEquals(objCareTeam[0].Member__r.ContactId, orderObj.implanting_physician__c);
        }
    }
    
SwethaSwetha (Salesforce Developers) 
HI Krishna,
You would need to create a test user which has a role defined, and then runas test user. With this all the account or contacts which are getting created will have Owner as this test user, and now when in your code the community user from your code it wont throw an error.

I see you are using System.runAs(adminUser).  You should be running in the context of  inserted 'user'  to fix the error.

See https://help.salesforce.com/s/articleView?id=000330124&type=1

https://salesforce.stackexchange.com/questions/102170/test-class-issue-with-unknown-exception-portal-account-owner-must-have-a-role

If this information helps, please mark the answer as best. Thank you
mukesh guptamukesh gupta
Hi Krishna

Please use below code:-
 
@isTest
    private static void VerifyPopulatePhysicianOnCareTeam(){
        User adminUser = [Select Id, UserRoleId From User Where Profile.Name='System Administrator' AND isActive = true Limit 1];
        UserRole role = new UserRole(DeveloperName = 'MyCustomRole', Name = 'My Role');
		insert role;
        System.runAs(adminUser){
            Test.startTest();
            Account businessAccount = TestUtility.createAccounts(1, 'Test', false)[0];
            businessAccount.Type = 'Customer';
            insert businessAccount;
            List<Account> listPersonAccount = TestUtility.createPatientAccounts(1, 'Patient Account', true);
            Id RecordTypeIdValue =SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Provider').getRecordTypeId();
            Contact physicianContact = new Contact(LastName = 'physician Contact', AccountId = businessAccount.Id , RecordTypeId = RecordTypeIdValue);
            insert physicianContact;
            User user = new User();
            user.ProfileID = [Select Id From Profile Where Profile.Name='Ext. Physician'].Id;
            user.EmailEncodingKey = 'ISO-8859-1';
            user.LanguageLocaleKey = 'en_US';
            user.TimeZoneSidKey = 'America/New_York';
            user.LocaleSidKey = 'en_US';
            user.FirstName = 'first';
            user.LastName = 'last';
            user.Username = 'test@edomain.com';
            user.CommunityNickname = 'testUser123';
            user.Alias = 't1';
            user.Email = 'no@email.com';
            user.UserRoleId = role.Id;
            user.IsActive = true;
            user.ContactId = physicianContact.Id;
            insert user; 
            Order orderObj = new Order();
            orderObj.EffectiveDate = system.today();
            orderObj.EndDate = system.today() + 4;
            orderObj.AccountId = businessAccount.Id;
            orderObj.patient_account__c=listPersonAccount[0].Id;
            orderObj.Implanting_physician__c=user.ContactId;
            orderObj.Type='NROT';
            insert orderObj;
            Test.stopTest();
            List<CareTeam__c> objCareTeam = [SELECT Id, Name, Member__r.ContactId, Patient__c 
                                             FROM CareTeam__c 
                                             WHERE Patient__c =: orderObj.patient_account__c LIMIT 1];
            System.assertEquals(objCareTeam[0].Member__r.ContactId, orderObj.implanting_physician__c);
        }
    }

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Krishna Sahu 1Krishna Sahu 1
Hi Mukesh
Not Working getting this error
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, role type must match user type: []
mukesh guptamukesh gupta
Hi Krishna,

Use below code:-
 
@isTest
    private static void VerifyPopulatePhysicianOnCareTeam(){
        User adminUser = [Select Id, UserRoleId From User Where Profile.Name='System Administrator' AND isActive = true Limit 1];
        UserRole role = new UserRole(DeveloperName = 'MyCustomRole', Name = 'My Role');
		insert role;
        adminUser.UserRoleId = role.Id;
        update adminUser;
        System.runAs(adminUser){
            Test.startTest();
            Account businessAccount = TestUtility.createAccounts(1, 'Test', false)[0];
            businessAccount.Type = 'Customer';
            insert businessAccount;
            List<Account> listPersonAccount = TestUtility.createPatientAccounts(1, 'Patient Account', true);
            Id RecordTypeIdValue =SObjectType.Contact.getRecordTypeInfosByDeveloperName().get('Provider').getRecordTypeId();
            Contact physicianContact = new Contact(LastName = 'physician Contact', AccountId = businessAccount.Id , RecordTypeId = RecordTypeIdValue);
            insert physicianContact;
            User user = new User();
            user.ProfileID = [Select Id From Profile Where Profile.Name='Ext. Physician'].Id;
            user.EmailEncodingKey = 'ISO-8859-1';
            user.LanguageLocaleKey = 'en_US';
            user.TimeZoneSidKey = 'America/New_York';
            user.LocaleSidKey = 'en_US';
            user.FirstName = 'first';
            user.LastName = 'last';
            user.Username = 'test@edomain.com';
            user.CommunityNickname = 'testUser123';
            user.Alias = 't1';
            user.Email = 'no@email.com';
            user.IsActive = true;
            user.ContactId = physicianContact.Id;
            insert user; 
            Order orderObj = new Order();
            orderObj.EffectiveDate = system.today();
            orderObj.EndDate = system.today() + 4;
            orderObj.AccountId = businessAccount.Id;
            orderObj.patient_account__c=listPersonAccount[0].Id;
            orderObj.Implanting_physician__c=user.ContactId;
            orderObj.Type='NROT';
            insert orderObj;
            Test.stopTest();
            List<CareTeam__c> objCareTeam = [SELECT Id, Name, Member__r.ContactId, Patient__c 
                                             FROM CareTeam__c 
                                             WHERE Patient__c =: orderObj.patient_account__c LIMIT 1];
            System.assertEquals(objCareTeam[0].Member__r.ContactId, orderObj.implanting_physician__c);
        }
    }

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh