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
Elsa RousselElsa Roussel 

ChatterAnswersAuthProviderRegTest : unjustified Code Coverage Failure

When I try to deploy some component (without any link to chatter) i have the error : 

"ChatterAnswersAuthProviderRegTest.validateCreateUpdateUser(), Details: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [ProfileId]: [ProfileId] Class.ChatterAnswersAuthProviderRegTest.validateCreateUpdateUser: line 31, column 1"

the test class ask for a ProfileId, while i can't modify it.

Name                            Version       Nom d'espace       Type
API        33.0             API           
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', '', 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];
      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, '', 'Email should have been same');
      Contact c = new Contact();
      c.AccountId = (newUser.Username.split('@'))[0];
      c.LastName = 'contactLast';
      newUser.Alias = 'firstusr';
      newUser.TimeZoneSidKey = 'America/Los_Angeles';
      newUser.LocaleSidKey = 'en_US';
      newUser.EmailEncodingKey = 'UTF-8';
      newUser.LanguageLocaleKey = 'en_US';
      newUser.ContactId = c.Id;
      // newUser.ProfileId = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1].Id;  [try to add this but i can't save]

      insert(newUser); // <= this is where the error is located :
      Auth.UserData updateUserData = new Auth.UserData('testId', 'updatedFirst', 'updatedLast',
      'updatedFirst updatedLast', '', 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, '', 'Email should have been updated');
Marc MEMarc ME
Hi Elsa,

I have the same problem. No sollution yet though. I'll let you know if i find something.
Kind regards,

Marc MEMarc ME

This may have something to do with it:
Marc MEMarc ME
I solved it for now by going to the sandbox and deleting everything from the class and then deploying it in the production environment. Not exactly a proper solution, but at least i can continue developing for now.
Elsa RousselElsa Roussel
Thanks Marc/Kasper for your fast answer.

I tried some method, but i couldn't fix it for now, even with the link above but I can't make it work and I can't delete nether emptying those class on the client profile he need it.

We already contacted Salesforce supports about this problem and are waiting for their answer.
Best regards,

Marc MEMarc ME
All i left in de test class was this: 
private class ChatterAnswersAuthProviderRegTest {
  static testMethod void validateCreateUpdateUser() {
This leaves the original class intact and working, but without any unit tests. This doesn't cause any problems for us though. I also opened a case but having this fixed might take weeks or months, and i can't wait that long.
Marc MEMarc ME
Salesforce support refused our case. Were you able to open one Elsa?
I've just activated Chatter answers and now I'm experiencing this when I try to make any deployments.  I've raised a case with Salesforce.

Has anyone resolved this?

Marc, the ChatterAnswersAuthProviderRegTest class doesn't exist in my sandbox but it is in production.  Did you create this class in sandbox without any unit tests and include this in the deployment?
Marc MEMarc ME
Correct, Allen. I created the test class in the sandbox and than deployed it without issue. Do keep a copy of the original test class though. You might need it in the future ;).
Thanks for the help.  I was able to overcome this by copying the class to our sandbox and adding the following line just before the insert:

newUser.ProfileId = '00eD0000001msUC';

NOTE: The ID is a hard-coded but this solves the issue.  

I have the same issue. It's pity that SalesForce refuses to solve cases like this one. Which was the reason to refuse it?

I am currently trying both solutions:
1) clear the test class as suggested by Marc ME. This is not a proper way to make things work, especially if the issue is generated by the system itself instead of by something on the user side.

2) hard-coding the ProfileId, as suggested by ab84. This solution is not working with our ORG: we are getting the error Error MessageSystem.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 TraceClass.ChatterAnswersAuthProviderRegTest.validateCreateUpdateUser: line 32, column 1
Ab, which profile is your 00eD0000001msUC hard coded profile?

Ok, the workaround 1 worked but this should be fixed by SalesForce as soon as possible. Removing all the code generating the errors is a great way to have trouble in the future.
Tim May 12Tim May 12
We still saw this including today.

Just in case anyone else comes here. The fix was to hard-code the Profile Id in the class to the "Chatter Answers" UserProfile Id.