trigger CountOfContactsRelatedToAccount on
Contact (after insert, after delete, after Undelete) {
Set<Id> accId = new Set<Id>();
if(Trigger.isInsert || Trigger.isUndelete){
for(Contact con : Trigger.new){
accId.add(con.AccountId);
}
}
if(Trigger.isDelete){
for(Contact con : Trigger.old){
accId.add(con.AccountId);
}
}
List<Account> accList = [Select Id,Name,Count_of_Contacts__c ,
(Select id from contacts) from Account where Id IN : accId];
for(Account acc :accList){
acc.Count_of_Contacts__c = acc.contacts.size();
}
update accList;
}
Thursday, April 6, 2023
Trigger for Count Number of Contacts Related To Account in Salesforce
Subscribe to:
Post Comments (Atom)
Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC)
Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC) Introduction: Salesforce Lightning Web ...
-
1. What is the purpose of writing the test class? After developing an apex class or apex trigger we should write the unit tests a...
-
1. Whenever a record is inserted to the account automatically inserted to the contact? Ans: trigger SCENARIO1 on Account (after inse...
-
Salesforce Certified Administrator Salesforce Certified Advanced Administrator Salesfo...
No comments:
Post a Comment