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
Kelly Logan (Raptek)Kelly Logan (Raptek) 

Testing with user specific Account info

We have some visualforce pages used in Community to display information. We need to limit the listing shown by the account of the current user. In our sandbox this works fine by pulling the user's id and then their accountId from the related Contact entry.
    list<User> uList = [SELECT contact.accountId FROM User WHERE Id = :UserInfo.getUserId()];
    Id currentAccountId = uList.isEmpty() ? null : uList[0].contact.accountId;


But this doesn't seem to work in the test code. The UserInfo returns null. I know I can create a user and use System.RunAs() to run the test in context of that user, but my code to set the Contact AccountId for that test user is not working. Seems like there should be a better way of doing this.

    Account acc = new Account(name='Test College',recordTypeId=AccountRecordTypeInfo .get('College/University').getRecordTypeId());
    insert acc;
    
    Profile p = [SELECT Id FROM Profile WHERE Name='College - FA&CC Combined'];
    User u = new User(Alias = 'standt', Email='standarduser@testorg.com',
    EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
    LocaleSidKey='en_US', ProfileId = p.Id,
    TimeZoneSidKey='America/Denver', UserName='standarduser@testorg.com');  

    List<Contact> conList = [Select Id, AccountId from Contact Where Id = :u.ContactId ];
    Contact con1 = conList.isEmpty() ? null : conList[0];
    con1.AccountId = acc.Id;

So the goal is to have the current user's Contact entry list the test Account value created. Right now this is not working as the assignment (con1.AccountId = acc.Id) is coming back with "System.NullPointerException: Attempt to de-reference a null object"

Any ideas?

Best Answer chosen by Kelly Logan (Raptek)
Kelly Logan (Raptek)Kelly Logan (Raptek)
Okay, got it working as far as the RunAs(), just needed to insert the User entry and then refresh it from the db. Account acc = new Account(name='Test College',recordTypeId=AccountRecordTypeInfo .get('College/University').getRecordTypeId()); insert acc; Id genRT = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('General').getRecordTypeId(); Contact con1 = new Contact(lastName='Contact', firstName = 'Test', recordTypeId=genRT, accountId=acc.Id); insert con1; Profile p = [SELECT Id FROM Profile WHERE Name='College - FA&CC Combined']; User u = new User(Alias = 'standt', Email='standarduser@testorg.com', EmailEncodingKey='UTF-8', LastName='Tester', FirstName='FA&CC combo', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id, Contact = con1, ContactId = con1.Id, TimeZoneSidKey='America/Denver', UserName='standarduser@testorg.com'); insert u; u = [Select Id, LastName, ContactId From User Where Id = :u.Id];

All Answers

VineetKumarVineetKumar
I guess this is your problem line. you are assigning a null value to the object, and referring null.field, which will throw you a null pointer exception
List<Contact> conList = [Select Id, AccountId from Contact Where Id = :u.ContactId ];
Contact con1 = conList.isEmpty() ? new Contact(): conList[0];
con1.LastName = 'TestUser';
con1.AccountId = acc.Id;
Last name is required field for contact
Kelly Logan (Raptek)Kelly Logan (Raptek)
A simpler question may be, what is the best way to create a linked user and contact?
VineetKumarVineetKumar
User by default cannot be linked to a contact, only owner Id is the field that has a reference to the user (which is automatically update)
Is there a custom user field on contact?
Kelly Logan (Raptek)Kelly Logan (Raptek)
Creating the contact and the user separately is fine, I just need a method that works. Specifically I need to be able to set up a user that I can put into System.RunAs() who has a related Contact field with the AccountId set to a specific value. Again, see the initial post for the queries that are run. I tried creating the contact first and then using the Contact Id in the user creation, but that is not working either. Account acc = new Account(name='Test College',recordTypeId=AccountRecordTypeInfo .get('College/University').getRecordTypeId()); insert acc; Id genRT = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('General').getRecordTypeId(); Contact con1 = new Contact(lastName='Contact', firstName = 'Test', recordTypeId=genRT, accountId=acc.Id); insert con1; Profile p = [SELECT Id FROM Profile WHERE Name='College - FA&CC Combined']; User u = new User(Alias = 'standt', Email='standarduser@testorg.com', EmailEncodingKey='UTF-8', LastName='Tester', FirstName='FA&CC combo' LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id, Contact = con1, TimeZoneSidKey='America/Denver', UserName='standarduser@testorg.com'); When I try to call System.RunAs(u), I get "System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Cannot create a portal user without contact: [ContactId]"
Kelly Logan (Raptek)Kelly Logan (Raptek)
Okay, got it working as far as the RunAs(), just needed to insert the User entry and then refresh it from the db. Account acc = new Account(name='Test College',recordTypeId=AccountRecordTypeInfo .get('College/University').getRecordTypeId()); insert acc; Id genRT = Schema.SObjectType.Contact.getRecordTypeInfosByName().get('General').getRecordTypeId(); Contact con1 = new Contact(lastName='Contact', firstName = 'Test', recordTypeId=genRT, accountId=acc.Id); insert con1; Profile p = [SELECT Id FROM Profile WHERE Name='College - FA&CC Combined']; User u = new User(Alias = 'standt', Email='standarduser@testorg.com', EmailEncodingKey='UTF-8', LastName='Tester', FirstName='FA&CC combo', LanguageLocaleKey='en_US', LocaleSidKey='en_US', ProfileId = p.Id, Contact = con1, ContactId = con1.Id, TimeZoneSidKey='America/Denver', UserName='standarduser@testorg.com'); insert u; u = [Select Id, LastName, ContactId From User Where Id = :u.Id];
This was selected as the best answer