0

UPDATE

  • NullPointerException error has been resolved (thanks @Adrian)
  • INVALID_PERSON_ACCOUNT_OPERATION error was resolved by adding a RecTypeId variable
  • Now I am confronted by an insert error: REQUIRED_FIELD_MISSING, Required fields are missing: [ContactId]: [ContactId] (stack trace is Class.lifeEventNotifyControllerTest.TestSOQL: line 24, column 1)

Since I am looking at the Person Account, dummyContact.Id isn't an option, so I tried dummyContact.PersonContactId but that threw the same error. What am I missing?

Updated Apex Test

@isTest
private class lifeEventNotifyControllerTest { 

    static testMethod void TestSOQL() {
        String loggedUserId = UserInfo.getUserId();
        String RecTypeId = [SELECT Id FROM RecordType WHERE (Name='Person Account') AND (SobjectType='Account')].Id;
        
        Account dummyContact = new Account();
        dummyContact.RecordTypeID = RecTypeId;
        dummyContact.Firstname = 'Test';
        dummyContact.Lastname = 'Contact';
        dummyContact.PersonBirthdate = date.today();
        dummyContact.Contact_Type__pc = 'Sponsor';
        INSERT dummyContact;

        Account dummyAcct = new Account();
        dummyAcct.Name = 'Test Company';
        dummyAcct.Custom__c = loggedUserId;
        INSERT dummyAcct;

        AccountContactRelation acr = new AccountContactRelation();
        acr.ContactId = dummyContact.PersonContactId;
        acr.AccountId = dummyAcct.Id;
        INSERT acr;

        lifeEventNotifyController testCont = new lifeEventNotifyController ();
        List<AccountContactRelation> dummyList = testCont.getLifeEvents();

        System.assert(dummyList  != null);
        
    }
}

Original Question:

I am brand new to Apex development and am learning about the importance of code coverage. Right now I have a very simple Apex class querying the AccountContactRelation object via SOQL. When trying to create an Apex test class, I continue to get the following error:

"System.NullPointerException: Attempt to de-reference a null object" error.

Browsing StackExchange I believe this error has to do with my Account and Contact objects not being initialized in the test class?

Apex Class

public with sharing class lifeEventNotifyController {
  @AuraEnabled
  public List<AccountContactRelation> getLifeEvents() {
    // get current user id
    String loggedUserId = UserInfo.getUserId();
    List<AccountContactRelation> lifeEventList = [SELECT Id, ContactId, Contact.Name, Contact.Birthdate, AccountId, Account.Name FROM AccountContactRelation WHERE Account.Custom__c = :loggedUserId AND (Contact.Days_to_Birthday__c <= 7 OR Contact.Days_to_Birthday__c = 365)];
    
    return lifeEventList;

  }
}

NOTE: In the above class, Days_to_Birthday__c is a formula field based on the Contact.Birthdate date field.

Apex Test Class

@isTest
private class lifeEventNotifyControllerTest { 

    static testMethod void TestSOQL() {
        String loggedUserId = UserInfo.getUserId();
        
        Contact dummyContact = new Contact();
        dummyContact.Firstname = 'Test';
        dummyContact.Lastname = 'Contact';
        dummyContact.Birthdate = date.today();
        INSERT dummyContact;

        Account dummyAcct = new Account();
        dummyAcct.Name = 'Test Company';
        dummyAcct.Custom__c = loggedUserId;
        INSERT dummyAcct;

        AccountContactRelation acr = new AccountContactRelation();
        acr.ContactId = dummyContact.Id;
        acr.Contact.Birthdate = dummyContact.Birthdate;
        acr.AccountId = dummyAcct.Id;
        acr.Account.Name = dummyAcct.Name;
        acr.Account.Custom__c = dummyAcct.Custom__c;
        INSERT acr;

        lifeEventNotifyController testCont = new lifeEventNotifyController ();
        List<AccountContactRelation> dummyList = testCont.getLifeEvents();

        System.assert(dummyList  != null);
        
    }
}
3
  • Please clearly indicate which line throws the error. Without a stack trace, it is much harder to help you.
    – Adrian Larson
    Commented Sep 23, 2021 at 21:44
  • 1
    I just saw this note, @AdrianLarson, thanks for the tip. I will do this next time. It was Class.lifeEventNotifyControllerTest.TestSOQL: line 25, column 1
    – JaredC
    Commented Sep 23, 2021 at 22:25
  • Welcome to Salesforce Stack Exchange (SFSE). Once you get this cleared up, please be sure to take the Tour, look in the Help Center, and contribute with answers to others' questions if you can. Also, when someone asks for clarification in the comments, or you come up with more information on your own (e.g., you tried something that did not work, you did something that gave a different error) please edit your question to add the information - it can be missed if you answer in a comment.
    – Moonpie
    Commented Sep 23, 2021 at 22:39

1 Answer 1

0

Your problem seems to be in how you set the AccountContactRelationship.AccountId lookup. Specifically, you are trying to set both that field (which you do correctly), but also set Account.Name and Account.Custom__c, which are incorrect. Simply remove the lines which set those fields, as they appear to be the only possible source of any NullPointerException.

3
  • Ah, that makes sense. Because the Account name has already been declared within the dummyAcct variable, I don't need to declare it again in AccountContactRelation as it's a junction object. Right on.
    – JaredC
    Commented Sep 23, 2021 at 22:05
  • I now have a new error: INVALID_PERSON_ACCOUNT_OPERATION which appears to be because I am trying to test on the Contact object rather than the Account object where IsPersonAccount = TRUE. I will troubleshoot that next, but in the meantime, thank you for your quick reply!
    – JaredC
    Commented Sep 23, 2021 at 22:07
  • Yeah sounds like you need to set a specific RecordType.
    – Adrian Larson
    Commented Sep 23, 2021 at 22:08

You must log in to answer this question.

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