• Eric B
  • NEWBIE
  • 20 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 6
    Replies
Hi,
I need to write a trigger that populates the “Key Fields Populated” custom field with the count of the total number of the following fields that are not null:
● FirstName
● LastName
● Email
● Phone
● Website
● Title
Any suggestions?
Thanks
  • March 21, 2019
  • Like
  • 0
Hi all,
i'am new to Apex and trying to write a trigger that  counts the number of contacts with phone number related to an account and populate it on a custom field on account (Contacts_With_Phone_Numbers__c). this is the code, the problem when add a new contact to the related list, the number of contacts field is not updated! any help please!?

trigger accountsRelatedContacts on Contact (After insert,after update){ 
        // create a set of contact Ids to querry against
        List<Contact> contactsWithPhone = new List<Contact>();
        Set<Id> accIds = new Set<Id>();
        for (Contact con : contactsWithPhone){
            accIds.add(con.AccountId);
        }
    
        // querry for all the accounts with related contacts in the set of contacts           
        List<Account> accountsRelatedContacts = [Select Id, 
                                                     Name,
                                                     Contacts_With_Phone_Numbers__c,
                                                     (Select Id from Contacts)                                                   
                                                       From Account
                                                      Where Id in :accIds];
    
        // Loop through accounts to find matching contacts with phone numbers
        for (Account c : accountsRelatedContacts){
            List<Contact> s = c.Contacts;
            System.debug('Account:' + c.Name + ' No. of Contacts: '+ s.size());
            c.Contacts_With_Phone_Numbers__c = s.size();
        }
        Update accountsRelatedContacts;      
}

Thanks
  • March 09, 2019
  • Like
  • 0
Hi,
I need to write a trigger that populates the “Key Fields Populated” custom field with the count of the total number of the following fields that are not null:
● FirstName
● LastName
● Email
● Phone
● Website
● Title
Any suggestions?
Thanks
  • March 21, 2019
  • Like
  • 0
Hi all,
i'am new to Apex and trying to write a trigger that  counts the number of contacts with phone number related to an account and populate it on a custom field on account (Contacts_With_Phone_Numbers__c). this is the code, the problem when add a new contact to the related list, the number of contacts field is not updated! any help please!?

trigger accountsRelatedContacts on Contact (After insert,after update){ 
        // create a set of contact Ids to querry against
        List<Contact> contactsWithPhone = new List<Contact>();
        Set<Id> accIds = new Set<Id>();
        for (Contact con : contactsWithPhone){
            accIds.add(con.AccountId);
        }
    
        // querry for all the accounts with related contacts in the set of contacts           
        List<Account> accountsRelatedContacts = [Select Id, 
                                                     Name,
                                                     Contacts_With_Phone_Numbers__c,
                                                     (Select Id from Contacts)                                                   
                                                       From Account
                                                      Where Id in :accIds];
    
        // Loop through accounts to find matching contacts with phone numbers
        for (Account c : accountsRelatedContacts){
            List<Contact> s = c.Contacts;
            System.debug('Account:' + c.Name + ' No. of Contacts: '+ s.size());
            c.Contacts_With_Phone_Numbers__c = s.size();
        }
        Update accountsRelatedContacts;      
}

Thanks
  • March 09, 2019
  • Like
  • 0