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
S_BatmanS_Batman 

Only count Primary Contacts who does not have Do Not Email Checked...

I currently have this trigger which counts contacts within an account that are marked as Primary contacts:

trigger PrimaryContactCount on Contact (after insert, after delete, after undelete, after update) {
    set<Id> accIds = new set<Id>();
    
    if(trigger.isinsert || trigger.isUpdate || trigger.ISundelete){
        for(Contact con: Trigger.new){
        if(Trigger.isInsert || Trigger.isUndelete || (con.Primary_Contact_del__c !=Trigger.oldMap.get(con.Id).Primary_Contact_del__c))
            accIds.add(con.AccountId);
            }
            }
            
            if(trigger.isUpdate || trigger.isDelete) {
            for(Contact con: Trigger.old){
            if(trigger.isDelete || (con.Primary_Contact_del__c != Trigger.oldMap.get(con.ID).Primary_Contact_del__c))
            accIds.add(con.AccountId);
            }
            }
            
            List<Account> accList = [select id, Number_of_Primary_Contacts__c, (Select Id, Primary_Contact_del__c from Contacts) from Account Where ID IN: accIds];
            
            for(Account acc : accList){
            system.debug('Contacts--->'+acc.contacts.size());
            acc.Number_of_Primary_Contacts__c = 0;
            for(Contact con : acc.Contacts) {
            if(con.Primary_Contact_del__c)
            acc.Number_of_Primary_Contacts__c++;
            }
            }
            update accList;
            }

Each contact also has a Do Not Email checkbox, is it possible to alter the trigger so it only counts the Primary Contact when the Do Not Email check box is False.
Best Answer chosen by S_Batman
Mahesh DMahesh D
Hi,

Please find the below modified trigger:
 
trigger PrimaryContactCount on Contact (after insert, after delete, after undelete, after update) {
    set<Id> accIds = new set<Id>();
    
    if(trigger.isinsert || trigger.isUpdate || trigger.ISundelete){
        for(Contact con: Trigger.new){
			if(Trigger.isInsert || Trigger.isUndelete || (con.Primary_Contact_del__c !=Trigger.oldMap.get(con.Id).Primary_Contact_del__c
															|| con.Do_Not_Check_Email__c !=Trigger.oldMap.get(con.Id).Do_Not_Check_Email__c ))
				accIds.add(con.AccountId);
		}
    }
            
	if(trigger.isUpdate || trigger.isDelete) {
		for(Contact con: Trigger.old){
			if(trigger.isDelete || (con.Primary_Contact_del__c != Trigger.newMap.get(con.ID).Primary_Contact_del__c
			                        || con.Do_Not_Check_Email__c !=Trigger.newMap.get(con.Id).Do_Not_Check_Email__c ))
			accIds.add(con.AccountId);
		}
	}

	if(!accIds.isEmpty()) {
		List<Account> accList = [select id, Number_of_Primary_Contacts__c, (Select Id, Primary_Contact_del__c, Do_Not_Check_Email__c from Contacts) from Account Where ID IN: accIds];

		for(Account acc : accList){
			system.debug('Contacts--->'+acc.contacts.size());
			acc.Number_of_Primary_Contacts__c = 0;
			for(Contact con : acc.Contacts) {
				if(con.Primary_Contact_del__c && !con.Do_Not_Check_Email__c)
					acc.Number_of_Primary_Contacts__c++;
			}
		}
		update accList;
	}
}

Please do let me know if it helps you.

Regards,
Mahesh