Hello I recently started my Internship as a salesforce developer and I'm having a very difficult time with test classes. But there is a perticular case that has really got me banging my head against a wall.

When you enable Chatter, a couple of classes get added to your Account. I've managed to create basic test classes for most of them but this one really got me stuck.

Here is the piece of code that was generated.

global class ChatterAnswersAuthProviderRegistration implements Auth.RegistrationHandler{
global User createUser(Id portalId, Auth.UserData data){
Savepoint sp = Database.setSavepoint();
String accountId = null;
try { 
    accountId = new ChatterAnswers().createAccount(data.firstname, data.lastname, Site.getAdminId());
} catch (Exception e) {
    return null;
User u = new User();
u.FirstName = data.firstname;
u.LastName = data.lastname;
u.Username = accountId + '@facebook.com';
u.Email = data.email;
u.CommunityNickname = data.email.substring(0,  data.email.indexOf('@')) + System.currentTimeMillis();
u.UserPermissionsChatterAnswersUser = true;
//If using this registration handler with Salesforce Community registration then specify the profileid to associated with the new user.
//This profile is used only for Salesforce Community site registration and not for standalone Force.com site registration.
//u.profileId = '';
Site.setPortalUserAsAuthProvider(u, accountId);
return u;

global void updateUser(Id userId, Id portalId, Auth.UserData data){
Savepoint sp = Database.setSavepoint();
User u = [SELECT Contact.email, Contact.firstName, Contact.lastName FROM User WHERE id=:userId];
u.Contact.email = data.email;
u.Contact.firstName = data.firstName;
u.Contact.lastName = data.lastName;
u.firstName = data.firstName;
u.lastName = data.lastName;
u.email = data.email;
try {
    update u;
    update u.Contact;
 } catch (Exception e) {

And here is the test class that I've got online from Salesforce developer website.

   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',

t testLast', 'no-reply@salesforce.com', null, 'testuserlong', 'en_US', 'facebook',
null, new Map<String, String>{'language' => 'en_US'});
      ChatterAnswersAuthProviderRegistration reg = new ChatterAnswersAuthProviderRegistration();
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';

newUser.Alias = 'firstusr';
newUser.TimeZoneSidKey = 'America/Los_Angeles';
newUser.LocaleSidKey = 'en_US';
newUser.EmailEncodingKey = 'UTF-8';
newUser.LanguageLocaleKey = 'en_US';
newUser.ContactId = c.Id;


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');

The problem with this is that it comes up with two errors. One of them is

Failed to create createContainerMember for containerId=1dc250000009zMVAAY: duplicate value found: duplicates value on record with id:

and the other one is

expecting a right parentheses, found ','

On the website it says

Make sure you put the right profile id (i.e the one that you use for actual Chatter answers user registration) in the ChatterAnswersAuthProviderRegistration.createUser() (line #28) method before you run the test.

but I have no idea where I can get the Profile ID from and how to make this code work. Any help will be appriciated

1 Answer 1


but I have no idea where I can get the Profile ID from and how to make this code work. Any help will be appriciated

You can get a profile with a query like this:

Profile p = [SELECT Id FROM Profile WHERE Name = 'NAME OF THE PROFILE'];

Then when creating a profile in newUser, you can just add the following that will assign the profile to that user:

newUser.ProfileId = p.Id

All's you're doing here is creating an instance of Profile, p, and getting the Id of a profile based off of the name. From there you can get the Id from the instance just by using p.Id. It's as simple as that. From there, you're just assigning that Id to the ProfileId of the instance newUser.

As for the other two errors, can you give any other information, such as line numbers?

  • Thank you very much I've added those two lines of code, What would I put on the "Name of the profile part" ? </p> the expecting a right parentheses, found ',' is on line 28 which is "insert(newUser);"
    – user26057
    Nov 3, 2015 at 13:51
  • Try removing the brackets. I've not seen that before. insert newUser and insert c should work just fine.
    – Dan Jones
    Nov 3, 2015 at 14:02

Your Answer

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