• Ajay Mohanan
  • NEWBIE
  • 60 Points
  • Member since 2019

  • Chatter
    Feed
  • 2
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 2
    Replies
To show total no of contact records on Account

trigger ContactsTotalDisplayOnAccount on Contact (after insert, after update, after delete, after undelete) {
//Contact can be added/deleted/undeleted

List<contact> contacts = Trigger.isDelete ? Trigger.old : Trigger.new;
set<Id> accId = new set<Id>();
// Set is used to store unique values
    
for(contact con : contacts) {
    If(con.AccountId != null) {
       accId.add(con.AccountId); 
    }    
}
List<account> acclist = new List<account>();

for(account acc : [Select Id,Name,Total_Number_of_Contacts__c,(Select Id from Contacts)
                   from Account 
                   where Id IN :accId]) {
    account acts = new Account();
    acts.Id = acc.Id ; 
    acts.Total_Number_of_Contacts__c = acc.contacts.size(); 
    acclist.add(acts);                   
}
update acclist;
}

 
  • March 06, 2021
  • Like
  • 0
trigger AccountIndustryAnnualRevenue on Account (before insert , before update) {
    List<Account> AccList = new List<Account>();
    for(Account Acc : Trigger.new) {
        If((Acc.Industry == 'Chemicals') && (Acc.AnnualRevenue <= 1200)){
           AccList.add(Acc);
        }    
        If(AccList.size() > 0) {
          Acc.addError ('When Industry is ‘Chemical’, annual revenue cannot be less than 1200'); // logic is incorrect. 
          
        }   
    }
}
  • March 06, 2021
  • Like
  • 0
To show total no of contact records on Account

trigger ContactsTotalDisplayOnAccount on Contact (after insert, after update, after delete, after undelete) {
//Contact can be added/deleted/undeleted

List<contact> contacts = Trigger.isDelete ? Trigger.old : Trigger.new;
set<Id> accId = new set<Id>();
// Set is used to store unique values
    
for(contact con : contacts) {
    If(con.AccountId != null) {
       accId.add(con.AccountId); 
    }    
}
List<account> acclist = new List<account>();

for(account acc : [Select Id,Name,Total_Number_of_Contacts__c,(Select Id from Contacts)
                   from Account 
                   where Id IN :accId]) {
    account acts = new Account();
    acts.Id = acc.Id ; 
    acts.Total_Number_of_Contacts__c = acc.contacts.size(); 
    acclist.add(acts);                   
}
update acclist;
}

 
  • March 06, 2021
  • Like
  • 0
trigger AccountIndustryAnnualRevenue on Account (before insert , before update) {
    List<Account> AccList = new List<Account>();
    for(Account Acc : Trigger.new) {
        If((Acc.Industry == 'Chemicals') && (Acc.AnnualRevenue <= 1200)){
           AccList.add(Acc);
        }    
        If(AccList.size() > 0) {
          Acc.addError ('When Industry is ‘Chemical’, annual revenue cannot be less than 1200'); // logic is incorrect. 
          
        }   
    }
}
  • March 06, 2021
  • Like
  • 0