You need to sign in to do that
Don't have an account?
S_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.
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.
Please find the below modified trigger:
Please do let me know if it helps you.
Regards,
Mahesh