I am attempting to update a custom Callable Contacts field on the Account object. The trigger will look at all contacts associated with an account, and contacts that have phone numbers will be included in the custom field. Contacts without phone numbers will not be counted. My solution was to basically look at all of the contacts associated with the account, and if any of their phone fields are not null I add them to a list. So far all of my logic appears to work.

When I system.debug the callableContacts list, the list size is the correct number of contacts. When I use system.debug(myContact.Account.Callable_Contacts__c);...the result is null even though it should be an integer value.

I am having trouble assigning the size of that list to the custom field on the account record. Please advise.

trigger CallableContacts on Contact (after insert, after update, after delete) {
List<Account> accList = new List<Account>();
if (trigger.isInsert || trigger.isUpdate) {
    for (Contact myContact : Trigger.new) {
        if (myContact.Id != null) {
            List<Contact> acctFromRelatedContact = [SELECT   Id,
                                                      FROM   Contact
                                                     WHERE   Id = :myContact.Id
                                                     LIMIT   1];
            List<Contact> callableContacts = [SELECT    Id 
                                                FROM    Contact
                                               WHERE    (Phone != null
                                                  OR     AssistantPhone != null
                                                  OR     HomePhone != null
                                                  OR     OtherPhone != null)
                                                 AND    AccountId = :acctFromRelatedContact.get(0).AccountId];
            accList = [SELECT Id,
                         FROM Account
                        WHERE Id = :myContact.AccountId];
            if (myContact.Account.Callable_Contacts__c != null) {
                for (Account acc : accList) {
                    myContact.Account.Callable_Contacts__c = callableContacts.size();                        
                update accList;                    


  • Apart from best practices not being implemented, the problem seems to be this line if (myContact.Account.Callable_Contacts__c != null) { because the myContact.Account.Callable_Contacts__c will always be null.
    – manjit5190
    Commented Jul 29, 2020 at 15:38
  • Why would it always be null? Apologies for the amateur code...I am still very new to Apex and am trying to learn it. Commented Jul 29, 2020 at 15:48
  • It's because the Trigger.new or Trigger.old contains only the current record's fields, not any related record fields (like Account) so they are always null.
    – manjit5190
    Commented Jul 29, 2020 at 15:56
  • ok, well I put that if statement in there to avoid the null pointer reference. how do I update the field without getting the null pointer exception? Commented Jul 29, 2020 at 16:22
  • Your code will require restructuring. I can help but by tomorrow. It's night here.
    – manjit5190
    Commented Jul 29, 2020 at 16:47


Browse other questions tagged .