1

I am conducting an Apex test for ChatterAnswersAuthProviderRegTest, as I run this test, I encounter the following error message.

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [ProfileId]: [ProfileId]

I am curious to know where to go about obtaining the the id of a Chatter Answers Profile. I have attempted to call ProfileId though it appears the profile Id's I have available do not seem to allow. Or any steps I need to take.

@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', '[email protected]', null, 'testuserlong', 'en_US', 'facebook',
            null, new Map{'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, '[email protected]', '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', '[email protected]', null, 'testuserlong', 'en_US', 'facebook',
            null, new Map{'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 protected]', 'Email should have been updated');
        }
    }
}
1
  • newUser object - requires a Custom Portal Profile with self reg enabled. Nov 12, 2019 at 22:05

2 Answers 2

1

You need to set a ProfileId on newUser. It is not clear what your reg.createUser method does, but the error message indicates you will need to set this reference explicitly.

Profile someProfile = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
newUser.profileId = someProfile.Id;
insert newUser;
1
  • This will not work - It requires a Custom Portal Profile with self reg enabled. Nov 12, 2019 at 22:04
1

This is a long running issue... It is a Custom Unmanaged Salesforce deployed piece of code that obviously does not need to meet the high standards they put on us, or it did once. Also has a number of hangups related to Portals/Sites, Portals were effectively replaced by Communities, and the evolution of them.

This workaround is also lacking in info: https://success.salesforce.com/issues_view?id=a1p300000008ZX2AAM It does not elude to all of the settings required to do self Registration.

Yes the User needs a ProfileId. But they need a Portal Profile. Which also means that the Profile Needs to be associated with a Portal/Community. And because it is Apex Registration, it will require self registration turned on.

  1. You will need to Enable Self Reg: https://domain.my.salesforce.com/_ui/networks/setup/NetworkSettingsPage?retURL=%2Fui%2Fsetup%2FSetup%3Fsetupid%3DNetworks&setupid=NetworkSettings

  2. Then Clone your standard portal profile into a custom one. (unfortunately that is all I have wrt the profile settings, the rest worked for me test method ran success)

  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 code should actually have this:

    Profile prof2 = [SELECT Id FROM Profile
        WHERE Name = 'Your Custom High Volume Customer Portal User Profile here'];
        // OR Name = 'Customer Portal Manager Standard']; //Do not use Standard Profile Name
    

    ...

  5. Then this:

    newUser.ProfileId = prof2.Id;
    

The actual answer is Salesforce should fix this!

Probably Best idea is delete this code and the main class it tests, only if you are not using it! Maybe nice to have for future use???

You must log in to answer this question.

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