0

I need to create a test class for a community user, so need to first create the user, then do my test as that user. I'm struggling to get the Account for the User though. What is wrong here? The last 2 debug statements return null.

public static User createCommunityUser(Id accountId) {
    system.debug('$$$$TestData createCommunityUser accountId: ' + accountId);
    if (accountId == null) {
        Account testAccount = new Account(name ='Test Account');
        insert testAccount;
        accountId = testAccount.Id;
        system.debug('$$$$TestData createCommunityUser accountId: ' + accountId);
    }
    // Query for profile Id
    Id profileId = [Select Id From Profile Where Name = 'Community Login Profile' LIMIT 1].Id;
    Contact con = new Contact(LastName ='testCon',AccountId = accountId);
    insert con;
    system.debug('$$$$TestData createCommunityUser con.accountid: ' + con.AccountId);
    String hashString = '1000' + String.valueOf(Datetime.now().formatGMT('yyyy-MM-dd HH:mm:ss.SSS'));
    Blob hash = Crypto.generateDigest('MD5', Blob.valueOf(hashString));
    String hexDigest = EncodingUtil.convertToHex(hash);

    // Create community user
    User communityUser = new User(FirstName = 'Test', LastName = 'User',
                                  Email = 'test.user@invalid.com',
                                  ContactId = con.Id,
                                  ProfileId = profileId, UserName = hexDigest + 'test-user@fakeemail.com',
                                  Alias = 'tuser1', CommunityNickName = hexDigest, TimeZoneSidKey = 'America/New_York',
                                  LocaleSidKey = 'en_US', EmailEncodingKey = 'ISO-8859-1', LanguageLocaleKey = 'en_US',
                                  Default_Chatter_Frequency__c = 'Weekly Digests');
    insert communityUser;
    system.debug('$$$$TestData createCommunityUser communityUser.contactId: ' + communityUser.contactId);
    system.debug('$$$$TestData createCommunityUser communityUser.contact.accountId: ' + communityUser.contact.accountId);
    system.debug('$$$$TestData createCommunityUser communityUser.accountId: ' + communityUser.accountId);
    return communityUser;
}
  • 1
    As novarg mentioned, you need to retrieve the User record in order to get any field value except for ID. The system will automatically set ID after an insert, but any other field must be explicitly retrieved. Also, is there a reason you're not using Site.createExternalUser()? That method creates the contact automatically. – David Cheng Nov 13 at 15:56
  • David do you have any example code? I actually need to create from a PersonAccount and had a look at the Site.CreatePersonAccountPortalUser but I couldn't get that to work either. – Irene Nov 13 at 20:54
2

Your last 2 debug statements return null because at this moment, your communityUser only has the values that you defined a few lines above.

If you query that user right after insert with all these fields, then you should have all the info that you need:

insert communityUser;
communityUser = [SELECT Id, ContactId, Contact.AccountId, AccountId FROM User WHERE Id = :communityUser.Id];
system.debug('$$$$TestData createCommunityUser communityUser.contactId: ' + communityUser.contactId);
system.debug('$$$$TestData createCommunityUser communityUser.contact.accountId: ' + communityUser.contact.accountId);
system.debug('$$$$TestData createCommunityUser communityUser.accountId: ' + communityUser.accountId);
return communityUser;
0

The second-to-last statement prints null, because you can't traverse relationships that way. You can only access the Contact relationship from the User if you use a relationship query.

The last statement prints null because you haven't set AccountId on the User.

  • One can't set the AccountId on the User - it is supposed to take it from the Contact, but it isn't, which is why I'm struggling. – Irene Nov 13 at 1:50
  • It probably doesn't get set until the contact is inserted. That would cause it to be null. – Nicholas Savini Nov 13 at 1:52
  • The contact has already been inserted before creating the user, and the contact has an AccountId – Irene Nov 13 at 2:09
  • I'm referring to the User. The User hasn't been inserted, so the AccountId on the User hasn't been assigned. – Nicholas Savini Nov 13 at 2:11
  • I've added the insert on the User, but it still doesn't populate the AccountId on the User – Irene Nov 13 at 2:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.