0

I Am getting this error

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, role type must match user type: [] Stack Trace: Class.TestDataFactory.createCommunityUser: line 41, column 1 Class.CyberSourcePaymentTest.setUp: line 9, column 1

@isTest
public without sharing class TestDataFactory {
    
    public static List<Account_Request__c> createAccountRequests(Integer num, Boolean isFreelancer, String email, String nric) {
        List<Account_Request__c> ARs = new List<Account_Request__c>();
        for(Integer i = 0; i < num; i++) {
            Account_Request__c AR = new Account_Request__c();
            AR.Company_Name__c = isFreelancer? null : 'test';
            AR.Company_Registration_Number__c = isFreelancer? null : '1234';
            AR.Date_of_Birth__c = System.today();
            AR.Email__c = email != null ? email : generateRandomString(10) + DateTime.now().getTime() + '@testorg.com';
            AR.Freelancer__c = isFreelancer;
            AR.Last_Name__c = 'test';
            AR.Mobile__c = '12345678';
            AR.NRIC_FIN__c = nric != null ? nric : generateRandomString(4);
            AR.Section__c = 'EO/AMP';            
            ARs.add(AR);
        }
        insert ARs;
        return ARs;
    }

    public static User createCommunityUser() {
        Account acc = createBusinessAccounts(1)[0];
        Contact con = createExternalContacts(1, acc.Id, null)[0];
        Profile cpProfile = [Select Id From Profile Where Name = 'Contractor Portal User'];
        UserRole r = new UserRole(DeveloperName = 'test', Name = 'test');
        insert r;
        String uniqueUserName = generateRandomString(10) + DateTime.now().getTime() + '@testorg.com';
        User u = new User(LastName = 'test',
                      Email = 'test@test.com',
                      Username = uniqueUserName,
                      ProfileId = cpProfile.Id,
                      UserRoleId = r.Id,
                      TimeZoneSidKey = 'Asia/Singapore',
                      LanguageLocaleKey = 'en_US',
                      EmailEncodingKey = 'ISO-8859-1',
                      LocaleSidKey = 'en_SG',
                      Alias = 'test',
                      ContactId = con.Id); 
        insert u;
        return u;
    }

1
  • 3
    Welcome to SFSE, soum! Would you have any reason to set the role? Can you try removing it? May 23 at 11:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy