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
Gopikrishnan NedumparambumanaGopikrishnan Nedumparambumana 

FIELD_INTEGRITY_EXCEPTION, Chatter Answers User is not allowed for this License Type

On my test class, I am getting the following error.
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Chatter Answers User is not allowed for this License Type.: [UserPermissions] 
Stack Trace: Class.ChatterAnswersAuthProviderRegTest.validateCreateUpdateUser: line 32, column 1


The class that does it is

@isTest
private class ChatterAnswersAuthProviderRegTest {
  static testMethod void validateCreateUpdateUser() {
    User thisUser = [ select Id from User where Id = :UserInfo.getUserId() ];
    System.runAs ( thisUser ) {
      Auth.UserData userData = new Auth.UserData('testId', 'testFirst', 'testLast',
      'testFirst testLast', 'no-reply@salesforce.com', null, 'testuserlong', 'en_US', 'facebook',
      null, new Map<String, String>{'language' => 'en_US'});
      ChatterAnswersAuthProviderRegistration reg = new ChatterAnswersAuthProviderRegistration();
      Profile[] p = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];
      User[] adminUser = [SELECT Id, Firstname, Lastname FROM User WHERE IsActive = true and ProfileId =: p[0].Id LIMIT 1];
      reg.setSiteAdminUserId(adminUser[0].Id);
      User newUser = reg.createUser(null, userData);
      System.assert(newUser != null, 'A new user should have been created');
      System.assertEquals(newUser.Firstname, 'testFirst', 'First name should have been same');
      System.assertEquals(newUser.Lastname, 'testLast', 'Last name should have been same');
      System.assertEquals(newUser.Email, 'no-reply@salesforce.com', 'Email should have been same');
      
      Contact c = new Contact();
      c.AccountId = (newUser.Username.split('@'))[0];
      c.LastName = 'contactLast';
      insert(c);
      
      newUser.profileId = p[0].Id;
      newUser.Alias = 'firstusr';
      newUser.TimeZoneSidKey = 'America/Los_Angeles';
      newUser.LocaleSidKey = 'en_US';
      newUser.EmailEncodingKey = 'UTF-8';
      newUser.LanguageLocaleKey = 'en_US';
      newUser.ContactId = c.Id;
      
      insert(newUser);
      
      
      Auth.UserData updateUserData = new Auth.UserData('testId', 'updatedFirst', 'updatedLast',
      'updatedFirst updatedLast', 'no-reply@new.salesforce.com', null, 'testuserlong', 'en_US', 'facebook',
      null, new Map<String, String>{'language' => 'en_US'});
      reg.updateUser(newUser.Id, null, updateUserData);
      
      User dbUser =  [SELECT Id, Firstname, Lastname, Email FROM User WHERE Id = :newUser.Id];
      System.assertEquals(dbUser.Firstname, 'updatedFirst', 'First name should have been updated');
      System.assertEquals(dbUser.Lastname, 'updatedLast', 'Last name should have been updated');
      System.assertEquals(dbUser.Email, 'no-reply@new.salesforce.com', 'Email should have been updated');
    }
  }
}


I think this is related to some permission with the profile "System Administrator".  Any idea which permission needs to be enabled the user?
 
Amit Chaudhary 8Amit Chaudhary 8
Hi,

Please try below code:-

Example 1 :-
@isTest(SeeAllData=True)
private class ChatterAnswersAuthProviderRegTest {
    static testMethod void validateCreateUpdateUser() {
        User thisUser = [ select Id from User where Id = :UserInfo.getUserId() ];
        System.runAs ( thisUser ) {
            Auth.UserData userData = new Auth.UserData('testId', 'testFirst', 'testLast',
            'testFirst testLast', 'no-reply@salesforce.com', null, 'testuserlong', 'en_US', 'facebook',
            null, new Map<String, String>{'language' => 'en_US'});
            ChatterAnswersAuthProviderRegistration reg = new ChatterAnswersAuthProviderRegistration();
            Profile[] p = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];
            User[] adminUser = [SELECT Id, Firstname, Lastname FROM User WHERE IsActive = true and ProfileId =: p[0].Id LIMIT 1];
            reg.setSiteAdminUserId(adminUser[0].Id);
            User newUser = reg.createUser(null, userData);
            System.assert(newUser != null, 'A new user should have been created');
            System.assertEquals(newUser.Firstname, 'testFirst', 'First name should have been same');
            System.assertEquals(newUser.Lastname, 'testLast', 'Last name should have been same');
            System.assertEquals(newUser.Email, 'no-reply@salesforce.com', 'Email should have been same');
            
            Contact c = new Contact();
            c.AccountId = (newUser.Username.split('@'))[0];
            c.LastName = 'contactLast';
            insert(c);
            
            newUser.Alias = 'firstusr';
            newUser.TimeZoneSidKey = 'America/Los_Angeles';
            newUser.LocaleSidKey = 'en_US';
            newUser.EmailEncodingKey = 'UTF-8';
            newUser.LanguageLocaleKey = 'en_US';
            newUser.ContactId = c.Id;
            
            insert(newUser);
            
            
            Auth.UserData updateUserData = new Auth.UserData('testId', 'updatedFirst', 'updatedLast',
            'updatedFirst updatedLast', 'no-reply@new.salesforce.com', null, 'testuserlong', 'en_US', 'facebook',
            null, new Map<String, String>{'language' => 'en_US'});
            reg.updateUser(newUser.Id, null, updateUserData);
            
            User dbUser =  [SELECT Id, Firstname, Lastname, Email FROM User WHERE Id = :newUser.Id];
            System.assertEquals(dbUser.Firstname, 'updatedFirst', 'First name should have been updated');
            System.assertEquals(dbUser.Lastname, 'updatedLast', 'Last name should have been updated');
            System.assertEquals(dbUser.Email, 'no-reply@new.salesforce.com', 'Email should have been updated');
        }
    }
}

Example 2:- Please check below post :-
https://success.salesforce.com/issues_view?id=a1p300000008XHfAAM

https://developer.salesforce.com/forums/?id=906F0000000BOfvIAG

Please let us know if this will help you.

Thanks
Amit Chaudhary
Gopikrishnan NedumparambumanaGopikrishnan Nedumparambumana
Thanks Amit for that quick reply!!
I saw that the code that you provided doens't have the profileId field on insertion. When I removed the profileId field from insertion, previously I got the error like the ProfileID is missing. Anyway, I will re-deploy it once again with new code to see if it shows again.
Gopikrishnan NedumparambumanaGopikrishnan Nedumparambumana
Hi Amit,

As said, the code throws the profile Id missing error.  Any clue what that is?

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [ProfileId]: [ProfileId] 
Stack Trace: Class.ChatterAnswersAuthProviderRegTest.validateCreateUpdateUser: line 31, column 1
Jamie BrowningJamie Browning

This is a long running issue... 
Go here for the Answer (not the Ticked Answer the Answer with decent info)

 1. You will need to Enable Self Reg in Community:
 2. Then Clone your standard portal profile into a custom one.
 3. Safety Sake you should ensure your portals do not have any self reg code that you do not intend to exist. Remove self reg pages from user profile.
 4. Then your adjust your code as per the answer by NZ DEV:
https://salesforce.stackexchange.com/a/284739/38277

The actual answer is Salesforce should fix this!
This response from salesforce in inadequate. Except the delete the code option will fits many orgs needs, but really...?
https://domain.my.salesforce.com/_ui/networks/setup/NetworkSettingsPage?retURL=%2Fui%2Fsetup%2FSetup%3Fsetupid%3DNetworks&setupid=NetworkSettings